java线程之二 多线程

单线程开发模式中。如果一个客户端没有与客户端断开连接,则其他的客户端就必须

一直等待。多线程则允许多个客户端同时连接服务端,而不需要等待其他客户端断开后

才可以连接,也就是通常所说的非阻塞通信。

服务端

 1 /**
 2  * @author  draem0507@gmail.com
 3  * @TODO    java线程开发之二 多线程
 4  * @version 1.0
 5  * @date 2013-5-7下午09:19:45    
 6  * @blgos http://www.cnblogs.com/draem0507
 7  */
 8 
 9 
10  class ServerTest {
11     private ServerSocket serverSocket;
12 
13     
14 
15     public ServerTest() {
16         try {
17             serverSocket = new ServerSocket(10000);
18             // 开启无限循环 创建每个客户端的socket线程
19             while (true) {
20                 Socket socket = serverSocket.accept();
21                 new ReceiveSocket(socket).start();
22             }
23         } catch (IOException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         }
27 
28     }
29 
30     public static void main(String[] args) {
31         new ServerTest();
32 
33     }
34 
35     private class ReceiveSocket extends Thread {
36         private Socket socket;
37 
38         public ReceiveSocket(Socket socket) {
39             this.socket = socket;
40         }
41 
42         private BufferedReader reader;
43         private PrintWriter writer;
44 
45         @Override
46         public void run() {
47 
48             try {
49                 reader = new BufferedReader(new InputStreamReader(socket
50                         .getInputStream()));
51                 writer = new PrintWriter(socket.getOutputStream(), true);
52                 // 开启无限循环 监控消息
53                 while (true) {
54                     String msg = reader.readLine();
55                     System.out.println("客户端对你说:" + msg);
56                     // write(msg)+write("\r")==println() 具体请参考PrintWriter源码
57                     writer.write("服务端收到客户端发送的信息:" + msg);
58                     writer.println();
59                     while (msg.equals("bye")) {
60                         break;
61                     }
62                 }
63 
64             } catch (IOException e) {
65                 // TODO Auto-generated catch block
66                 e.printStackTrace();
67             } finally {
68                 if (null != reader) {
69                     try {
70                         reader.close();
71                     } catch (IOException e) {
72                         // TODO Auto-generated catch block
73                         e.printStackTrace();
74                     }
75                 }
76                 if (null != writer) {
77                     try {
78                         reader.close();
79                     } catch (IOException e) {
80                         // TODO Auto-generated catch block
81                         e.printStackTrace();
82                     }
83                 }
84                 try {
85                     socket.close();
86                 } catch (IOException e) {
87                     // TODO Auto-generated catch block
88                     e.printStackTrace();
89                 }
90             }
91 
92         }
93 
94     }
95 
96 }

客户端

 1 public class ClientTest {
 2     public static void main(String[] args) throws Exception {
 3         Socket socket = new Socket("localhost", 10000);
 4         BufferedReader in = new BufferedReader(new InputStreamReader(socket
 5                 .getInputStream()));
 6         PrintWriter out = new PrintWriter(socket.getOutputStream());
 7         BufferedReader reader = new BufferedReader(new InputStreamReader(
 8                 System.in));
 9 
10         while (true) {
11             String msg = reader.readLine();
12             out.println(msg);
13             out.flush();
14             if (msg.equals("bye")) {
15                 break;
16             }
17             System.out.println(in.readLine());
18         }
19         socket.close();
20     }
21 }

运行服务端后,可以多次运行客户端,并且之间的通信是互不干扰。

顺便提个问题,如果实现客户端与客户端的通信 及其服务端如何群发信息给客户端,感兴趣的朋友可以去研究下。

 

 

posted @ 2013-05-08 11:50  draem0507  阅读(289)  评论(0编辑  收藏  举报
View Code