TCP程序设计(单线程服务器和多线程服务器学习)

/********下面是单线程服务器的服务器端*********/

package serverSocket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/***
 * TCP程序设计:
 * java中使用套接字完成TCP程序的开发,特点:可靠、双向、持续、点对点
 * 对java的网络程序来说,每一个客户端都是一个socket对象;
 * serverSocket编程服务器端:
 * 注意:该为单线程服务器,即一个客户连接进入之后,其他客户只能在等待,连接不进去的.
 * 注意:该种服务器设置,不适合聊天软件的开发,因为必须等待连接进入,会占用大量资源.
 * @author Administrator
 *
 */
public class EchoServer {
 public static void main(String[] args) throws Exception{
  ServerSocket server = null;
  Socket client = null;
  PrintStream out = null;
  BufferedReader buf = null;
  server = new ServerSocket(8888);
  while(true){
   System.out.println("服务器运行,等待客户连接中......");
   client = server.accept();//排队等待客户端接入
   buf = new BufferedReader(
     new InputStreamReader(client.getInputStream()));
   out = new PrintStream(client.getOutputStream());
   
   boolean flag = true;
   while(flag){
    String str = buf.readLine();
    if(str == null || "".equals(str)){
     flag = false;
    }else{
     if("bye".equals(str)){
      flag = false;
     }else{
      out.println("ECHO: " + str);
     }
    }
   }
   buf.close();
   out.close();
   client.close();
  }
 }
}


/********下面是单线程服务器的客户端*********/

package serverSocket;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
/***
 * socket通信客户端:
 * 关键:
 * 1、input:控制台输入流,
 * 2、out:向服务器输出流,
 * 3、buf:读取返回信息流
 * @author Administrator
 *
 */
public class EchoClient {
 public static void main(String[] args) throws Exception{
  Socket client = null;
  client = new Socket("localhost",8888);//连接编程,
  BufferedReader buf = null;
  PrintStream out = null;
  BufferedReader input = null;
  
  input = new BufferedReader(//输入流:接受从控制台输入的信息,
     new InputStreamReader(System.in));
  out = new PrintStream(client.getOutputStream());//输出流:打开向服务器输出的流
  buf = new BufferedReader(//输入流:接受从服务器返回的信息,
     new InputStreamReader(client.getInputStream()));
  
  boolean flag = true;
  while(flag){
   System.out.println("输入信息: ");
   String line = input.readLine();
   out.println(line);
   if("bye".equals(line)){
    flag = false;
   }else{
    String echo = buf.readLine();//获取服务器返回信息,
    System.out.println(echo);
   }
  }
  client.close();
  buf.close();
  out.close();
  input.close();
 }
}


/********下面是多线程服务器测试*********/

package serverSocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/***
 * 多线程服务器:
 * 原理:每当接入一客户,就另起一线程,将回复信息的任务交给了线程处理,
 * 注意:该种服务器设置,不适合聊天软件的开发,因为必须等待连接进入,会占用大量资源.
 * @author Administrator
 *
 */
public class EchoThreadServer {
 public static void main(String[] args)throws Exception {
  ServerSocket server = null;
  Socket client = null;
  server = new ServerSocket(8888);
  boolean flag = true;
  while(flag){
   System.out.println("服务器正在运行,等待客户连接......");
   client = server.accept();
   new Thread(new EchoThread(client)).start();
   //server.close();
  }
 }
}
/***
 * 每一客户接入,都新创建一线程,打开输入流,输出流,处理信息
 * @author Administrator
 *
 */
class EchoThread implements Runnable{
 private Socket client = null;
 public EchoThread(Socket client) {
  this.client = client;
 }
 @Override
 public void run(){
  PrintStream out = null;
  BufferedReader buf = null;
  
  try {
   out = new PrintStream(client.getOutputStream());//打开输出流
   buf = new BufferedReader(new InputStreamReader(client.getInputStream()));//打开输入流
   
   boolean flag = true;
   while(flag){
    String line = buf.readLine();
    if(line == null || "".equals(line)){
     flag = false;
    }else{
     if("bye".equals(line)){
      flag = false;
     }else{
      out.println("Echo:  " + line);
     }
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally{
   try {
    out.close();
    buf.close();
    client.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}
*******************************************************经测试通过,只为交流学习************************

你可能感兴趣的:(J2EE)