新建兩個工程,一個客戶端,一個服務(wù)端,先啟動服務(wù)端再啟動客戶端
兩個工程的讀寫操作線程類基本上完全相同
服務(wù)端:
1
2
3
4
5
6
7
8
9
10
11
12
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Server { public static final int PORT = 8000 ; //監(jiān)聽的端口號 public static void main(String[] args) { Server server = new Server(); server.init(); } public void init() { ServerSocket serverSocket = null ; try { serverSocket = new ServerSocket(PORT); while ( true ) { Socket client = serverSocket.accept(); //一個客戶端連接就開戶兩個線程處理讀寫 new Thread( new ReadHandlerThread(client)).start(); new Thread( new WriteHandlerThread(client)).start(); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (serverSocket != null ){ serverSocket.close(); } } catch (IOException e) { e.printStackTrace(); } } } } /* *處理讀操作的線程 */ class ReadHandlerThread implements Runnable{ private Socket client; public ReadHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataInputStream dis = null; try{ while(true){ //讀取客戶端數(shù)據(jù) dis = new DataInputStream(client.getInputStream()); String reciver = dis.readUTF(); System.out.println("客戶端發(fā)過來的內(nèi)容:" + reciver); } }catch(Exception e){ e.printStackTrace(); }finally{ try { if(dis != null){ dis.close(); } if(client != null){ client = null; } } catch (IOException e) { e.printStackTrace(); } } } } /* * 處理寫操作的線程 */ class WriteHandlerThread implements Runnable{ private Socket client; public WriteHandlerThread(Socket client) { this .client = client; } @Override public void run() { DataOutputStream dos = null ; BufferedReader br = null ; try { while ( true ){ //向客戶端回復(fù)信息 dos = new DataOutputStream(client.getOutputStream()); System.out.print( "請輸入:\t" ); // 鍵盤錄入 br = new BufferedReader( new InputStreamReader(System.in)); String send = br.readLine(); //發(fā)送數(shù)據(jù) dos.writeUTF(send); } } catch (Exception e){ e.printStackTrace(); } finally { try { if (dos != null ){ dos.close(); } if (br != null ){ br.close(); } if (client != null ){ client = null ; } } catch (IOException e) { e.printStackTrace(); } } } } |
客戶端:
1
2
3
4
5
6
7
8
9
10
11
12
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
100
101
102
103
104
105
106
107
108
109
110
|
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; public class Client { public static final String IP = "localhost" ; //服務(wù)器地址 public static final int PORT = 8000 ; //服務(wù)器端口號 public static void main(String[] args) { handler(); } private static void handler(){ try { //實例化一個Socket,并指定服務(wù)器地址和端口 Socket client = new Socket(IP, PORT); //開啟兩個線程,一個負責讀,一個負責寫 new Thread( new ReadHandlerThread(client)).start(); new Thread( new WriteHandlerThread(client)).start(); } catch (Exception e) { e.printStackTrace(); } } } /* *處理讀操作的線程 */ class ReadHandlerThread implements Runnable{ private Socket client; public ReadHandlerThread(Socket client) { this.client = client; } @Override public void run() { DataInputStream dis = null; try { while(true){ //讀取服務(wù)器端數(shù)據(jù) dis = new DataInputStream(client.getInputStream()); String receive = dis.readUTF(); System.out.println("服務(wù)器端返回過來的是: " + receive); } } catch (IOException e) { e.printStackTrace(); } finally{ try { if(dis != null){ dis.close(); } if(client != null){ client = null; } } catch (IOException e) { e.printStackTrace(); } } } } /* * 處理寫操作的線程 */ class WriteHandlerThread implements Runnable{ private Socket client; public WriteHandlerThread(Socket client) { this .client = client; } @Override public void run() { DataOutputStream dos = null ; BufferedReader br = null ; try { while ( true ){ //取得輸出流 dos = new DataOutputStream(client.getOutputStream()); System.out.print( "請輸入: \t" ); //鍵盤錄入 br = new BufferedReader( new InputStreamReader(System.in)); String send = br.readLine(); //發(fā)送數(shù)據(jù) dos.writeUTF(send); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (dos != null ){ dos.close(); } if (br != null ){ br.close(); } if (client != null ){ client = null ; } } catch (Exception e){ e.printStackTrace(); } } } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/zlqqhs/article/details/8757250