(一)第一个例子是简单的Socket通信,一个客户端和一个服务器线程:
/** * SimpleClient.java */ package network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class SimpleClient { public static void main(String[] args) { Socket socket = null; BufferedReader reader = null; PrintWriter writer = null; try { //请求建立通信套接字 socket = new Socket("localhost",8089); //获取输入输出流 reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream(), true); //true means autoFlush //while(true){ writer.println("hello, I am Client\n"); String mess = reader.readLine(); System.out.print("Client Receive:"+mess); //} } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if(socket != null) socket.close(); if(reader != null) reader.close(); } catch (IOException e) { e.printStackTrace(); } if(writer != null) writer.close(); } } }
/** * SimpleServer.java */ package network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SimpleServer { public static void main(String[] args) { ServerSocket ss = null; //用于接收请求的Socket Socket socket = null; //用于通信的Socket BufferedReader reader = null; PrintWriter writer = null; try { ss = new ServerSocket(8089); System.out.print("waiting for new request...\n"); socket = ss.accept(); //等待请求,建立通信Socket reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream(), true); String mess = reader.readLine(); System.out.print("Server Receive:"+mess); writer.println("Yes, I can hear you"); } catch (IOException e) { e.printStackTrace(); } } }
(二)第二个列子是在服务器端,采用多线程方式处理来自客户端的请求:
/** * TCPClient.java */ package network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class TCPClient { private static int id = 0; public static void main(String[] args) { String host = "localhost"; int port = 8060; Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { //创建五个请求服务器连接的客户端 for(int i=0;i<5;i++){ //创建通信套接字 socket = new Socket(host, port); //获取套接字的输入输出流 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); //向服务器发送消息 out.println("hello, i am client "+(++id)); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ System.out.print("Closing Socket...\n"); try { if(socket != null) socket.close(); if(in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } if(out != null) out.close(); } } }
/** * TCPServer.java */ package network; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class TCPServer { public static void main(String[] args) { int port = 8060; Socket socket = null; ServerSocket server = null; try { //创建服务器套接字 server = new ServerSocket(port); while(true){ //接收来自客户端的请求,建立通信套接字 System.out.print("waiting for new request...\n"); socket = server.accept(); //创建一个线程处理该请求 Thread t = new Thread(new ServerThread(socket)); t.start(); } } catch (IOException e) { e.printStackTrace(); }finally{ try { if(server != null) server.close(); if(socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 处理来自客户端请求的线程 * */ class ServerThread implements Runnable{ private Socket socket; public ServerThread(Socket s){ this.socket = s; } public void run() { BufferedReader in = null; PrintWriter out = null; try { //获取套接字的输入输出流 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); //读取客户端发送来的消息 String s = in.readLine(); System.out.println("recv:"+s); //out.write("accept"); } catch (IOException e) { e.printStackTrace(); }finally{ System.out.print("Closing Connection...\n"); try { if(socket != null) socket.close(); if(in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } if(out != null) out.close(); } } }