io流+网络+线程池 实现简单的多客户端与服务器端通信

 

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.io.OutputStream;
 4 import java.net.Socket;
 5 import java.util.Scanner;
 6 //客户端要做的事就是 读出服务器端输出的信息,将要说的话写到服务器端
 7 public class Client {
 8 public static void main(String[] args) {
 9     Socket client = null;
10     OutputStream os  =null;
11     InputStream is = null;
12     Scanner sc= new Scanner(System.in);
13     
14     try {
15         client= new Socket("127.0.0.1",5000);
16         String address = client.getInetAddress().getHostAddress();
17         is = client.getInputStream();
18         int len=0;
19         byte []b = new byte[1024];
20         len=is.read(b, 0, b.length);
21         System.out.println(new String(b,0,len));
22         os = client.getOutputStream();
23         String sendMsg=sc.next();
24         os.write(sendMsg.getBytes());
25         while(true) {
26             len=is.read(b, 0, b.length);
27             String msg = new String(b,0,len);
28             System.out.println(msg);
29             String send = sc.next();
30             os.write(send.getBytes());
31             os.flush();
32             if(send.contains("bye")) //判断写出的字符串中是否包含bye 包含则退出
33             {
34                 break;
35             }
36         }
37         
38     } catch (Exception e) {
39         e.printStackTrace();
40     }finally {
41         try {
42             is.close();
43             os.close();
44             client.close();
45         } catch (IOException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         }
49     }
50 }
51 }

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.io.OutputStream;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 import java.util.Scanner;
 7 import java.util.concurrent.ExecutorService;
 8 import java.util.concurrent.Executors;
 9 //服务器端接收多个客户端的连接 并处理每个连接要做的事情
10 public class Server {
11     public static ServerSocket server;
12     public static Socket client;

        //静态代码块只执行一次 跟随类加载
        //非静态匿名代码块 每次都跟随对象加载

13     static {
14         try {
15             server = new ServerSocket(5000);
16             System.out.println("服务器端已经创建,开放端口为5000");
17         } catch (IOException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20         }
21     }
//使用的线程池是newCachedThreadPool
22 public static void main(String[] args) { 23 ExecutorService exe = Executors.newCachedThreadPool(); 24 while(true) { 25 try { 26 client= server.accept(); 27 exe.execute(new SocketThread(client)); 28 29 30 } catch (IOException e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } 34 } 35 } 36 37 } 38 class SocketThread extends Thread { 39 Scanner sc= new Scanner(System.in); 40 private Socket s = null; 41 private InputStream is= null; 42 private OutputStream os= null; 43 public SocketThread(Socket s) { 44 this.s = s ; 45 46 } 47 @Override 48 public void run() { 49 try { 50 os = s.getOutputStream(); 51 os.write("你好 请输入你的名字".getBytes()); 52 os.flush(); 53 is=s.getInputStream(); 54 byte []b = new byte[1024]; 55 int len=is.read(b, 0, b.length); 56 //为线程设置名字 57 setName(new String (b,0,len)); 58 os.write("恭喜设置昵称成功,我们开始聊天吧".getBytes()); 59 os.flush(); 60 while(true) { 61 len=is.read(b, 0, b.length); 62 String msg = new String(b,0,len); 63 if(msg.contains("bye")) { 64 break; 65 } 66 System.out.println(getName()+":"+msg); 67 String send =sc.next(); 68 os.write(send.getBytes()); 69 os.flush(); 70 71 } 72 73 } catch (Exception e) { 74 // TODO: handle exception 75 } 76 finally { 77 try { 78 os.close(); 79 is.close(); 80 s.close(); 81 } catch (IOException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 } 86 } 87 }

 

posted @ 2020-09-02 21:11  jamers  阅读(269)  评论(0编辑  收藏  举报