简单的多线程实例

实现了在服务器端通过两个线程来监听socket请求和处理socket请求,并通过for循环来模拟整个时间段,当启动server程序后,每运行一次client程序都会在server端得到相应,代码如下:

server端:

 1 import java.io.DataInputStream;
 2 import java.io.DataOutputStream;
 3 import java.io.IOException;
 4 import java.net.ServerSocket;
 5 import java.net.Socket;
 6 
 7 class DealThread extends Thread{ // socket请求处理线程
 8     private Socket socket;
 9     DealThread(Socket socket){
10         super();
11         this.socket = socket;
12     }
13     public void run(){
14         try {
15         //用于向客户端发送数据的输出流
16         DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
17         //用于接收客户端发来的数据的输入流
18         DataInputStream dis = new DataInputStream(socket.getInputStream());
19         System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
20         //服务器向客户端发送连接成功确认信息
21         dos.writeUTF("接受连接请求,连接成功!");
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }
26 }
27 class ListenerThread extends Thread{ // 监听socket连接的线程
28 
29     ListenerThread(){
30         super();
31     }
32     public void run() {         
33              ServerSocket ss = null;
34         try {
35             ss = new ServerSocket(8888);
36             while(true)
37             {
38                 //服务器接收到客户端的数据后,创建与此客户端对话的Socket
39                 Socket socket = ss.accept();
40 
41                 // socket处理线程
42                 DealThread d = new DealThread(socket); 
43                 d.start();
44                 /*//用于向客户端发送数据的输出流
45                 DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
46                 //用于接收客户端发来的数据的输入流
47                 DataInputStream dis = new DataInputStream(socket.getInputStream());
48                 System.out.println("服务器接收到客户端的连接请求:" + dis.readUTF());
49                 //服务器向客户端发送连接成功确认信息
50                 dos.writeUTF("接受连接请求,连接成功!");*/
51                 //不需要继续使用此连接时,关闭连接
52                 if(socket.isClosed())
53                     socket.close();
54                 if(ss.isClosed())
55                     ss.close();
56             }
57         } catch (IOException e) {
58             e.printStackTrace();
59         }
60     }
61 }
62 
63 
64 public class server {
65 
66     /**
67      * 注意:Socket的发送与接收是需要同步进行的,即客户端发送一条信息,服务器必需先接收这条信息,
68      *      而后才可以向客户端发送信息,否则将会有运行时出错。
69      * @param args
70      * @throws IOException 
71      */
72     public static void main(String[] args) {
73 
74         ListenerThread l = new ListenerThread();
75         l.start();
76         for(int i = 0;i<100;i++)  // 模拟时间片的过程
77         {
78             try {
79                 Thread.sleep(1000);
80             } catch (InterruptedException e) {
81                 // TODO Auto-generated catch block
82                 e.printStackTrace();
83             }
84             System.out.println(i);
85         }
86     }
87 
88 }


client端:

 1 import java.io.DataInputStream;
 2 import java.io.DataOutputStream;
 3 import java.io.IOException;
 4 import java.io.OutputStream;
 5 import java.net.Socket;
 6 import java.net.UnknownHostException;
 7 
 8 public class Client {
 9 
10     /**
11      * @param args
12      */
13     public static void main(String[] args) {
14         Socket socket = null;
15         try {
16             socket = new Socket("localhost",8888);
17             //获取输出流,用于客户端向服务器端发送数据
18             DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
19             //获取输入流,用于接收服务器端发送来的数据
20             DataInputStream dis = new DataInputStream(socket.getInputStream());
21             //客户端向服务器端发送数据
22             dos.writeUTF("我是客户端,请求连接!");
23             //打印出从服务器端接收到的数据
24             System.out.println(dis.readUTF());
25             //不需要继续使用此连接时,记得关闭哦
26             socket.close();
27         } catch (UnknownHostException e) {
28             System.out.println("没有找到服务器");
29             e.printStackTrace();
30         } catch (IOException e) {
31             System.out.println("没有找到IO流");
32             e.printStackTrace();
33         }
34     }
35 
36 }
37  

输出结果为:

server端:

0
1
2
3
4
服务器接收到客户端的连接请求:我是客户端,请求连接!  (每次运行一次client程序时出现)
5
6
7
8
9
10
11
12
服务器接收到客户端的连接请求:我是客户端,请求连接! (每次运行一次client程序时出现)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99

client端:

接受连接请求,连接成功!

原文地址:https://www.cnblogs.com/xuxu8511/p/2505784.html