java socket 编程 多线程异步通信
服务器端代码:
1 package internetCommunication; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.ServerSocket; 8 import java.net.Socket; 9 10 public class Server { 11 12 public static void main(String[] args) { 13 try { 14 ServerSocket server = new ServerSocket(8888); 15 Socket client = server.accept(); 16 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 17 PrintWriter out=new PrintWriter(client.getOutputStream()); 18 BufferedReader userin = new BufferedReader(new InputStreamReader(System.in)); 19 20 21 new ReceiveTread(server,in,out,userin,client).start(); 22 new SendThread(out, userin,true).start(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 27 28 } 29 30 }
客户端代码
1 package internetCommunication; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.InetAddress; 8 import java.net.Socket; 9 10 public class Client { 11 12 13 public static void main(String[] args) { 14 try { 15 Socket server = new Socket(InetAddress.getLocalHost(), 8888); 16 BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream())); 17 PrintWriter out=new PrintWriter(server.getOutputStream()); 18 BufferedReader userin = new BufferedReader(new InputStreamReader(System.in)); 19 20 new SendThread(out,userin,false).start(); 21 new ReceiveTread(server,in,out,userin).start(); 22 23 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 28 29 } 30 }
发消息多线程类
1 package internetCommunication; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 public class SendThread extends Thread{ 10 11 PrintWriter out; 12 BufferedReader userin; 13 boolean isServer; 14 15 public SendThread(PrintWriter out,BufferedReader userin,boolean isServer) { 16 this.out = out; 17 this.userin = userin; 18 this.isServer = isServer; 19 } 20 21 @Override 22 public void run() { 23 SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 24 25 try { 26 while(true){ 27 if(isServer){ 28 out.println("Server "+sf.format(new Date())+"\n\t"+ userin.readLine()); 29 }else{ 30 out.println("client "+sf.format(new Date())+"\n\t"+ userin.readLine()); 31 } 32 out.flush(); 33 } 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 38 } 39 40 41 42 43 44 45 }
接消息多线程类
1 package internetCommunication; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class ReceiveTread extends Thread{ 10 BufferedReader in ; 11 ServerSocket server; 12 PrintWriter out; 13 BufferedReader userin; 14 Socket client; 15 16 17 public ReceiveTread(ServerSocket server,BufferedReader in,PrintWriter out,BufferedReader userin,Socket client) { 18 this.in = in; 19 this.server = server; 20 this.client = client; 21 this.out = out; 22 this.userin = userin; 23 } 24 25 public ReceiveTread(Socket client ,BufferedReader in,PrintWriter out,BufferedReader userin) { 26 this.in = in; 27 this.client = client; 28 this.out = out; 29 this.userin = userin; 30 } 31 32 33 @Override 34 public void run() { 35 try { 36 while(true){ 37 String info = in.readLine(); 38 while(info !=null){ 39 System.out.println(info); 40 info = in.readLine(); 41 } 42 if(in.readLine().equals("end")){ 43 break; 44 } 45 } 46 in.close(); 47 out.close(); 48 userin.close(); 49 if(client != null){ 50 client.close(); 51 } 52 server.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 }
以下是控制输出:
这是客户端的
这是服务器的:
这里要注意的是: 1、启动时要先启动服务器,在启动客户端,因为只有服务器启动了,开始监听某个端口,客户端才能连上
2、因为输入控制台和输出控制台都揉在一起显示,控制台显示接收消息的时候,光标还是会停留在上一次输入的地方,如果输入非汉字类的,光标会自动跳下来
但是输入汉字,会出现把接收到消息也发送的情况,所以需要输入的时候将光标自己点下来,如果是GUI编程的话,就没有这个问题,因为输入的面板和显示面板是独立开来的
转载自:https://www.cnblogs.com/cocohxq/archive/2012/09/28/2707104.html