一、創(chuàng)建TCP傳輸?shù)?a href="/article/30788.html">客戶端
1、建立TCP客戶端的Socket服務(wù),使用的是Socket對象,建議該對象一創(chuàng)建就明確目的地,即要連接的主機(jī);
2、如果連接建立成功,說明數(shù)據(jù)傳輸通道已建立,該通道就是Socket流,是底層建立好的,既然是流,說著這里既有輸入流,又有輸出流,想要輸入流或者輸出流對象,可以通過Socket來獲取,可以通過getOutputStream()和getInputStream()來獲取;
3、使用輸出流,將數(shù)據(jù)寫出;
4、關(guān)閉Socket服務(wù)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; public class Client { public static void main(String[] args) throws IOException { // 1、創(chuàng)建客戶端的Socket服務(wù) Socket socket = new Socket( "192.168.1.100" , 10002 ); // 2、獲取Socket流中輸入流 OutputStream out = socket.getOutputStream(); // 3、使用輸出流將指定的數(shù)據(jù)寫出去 out.write( "TCP is coming !" .getBytes()); // 4、關(guān)閉Socket服務(wù) socket.close(); } } |
二、創(chuàng)建TCP傳輸?shù)?a href="/article/90424.html">服務(wù)端
1、建立TCP服務(wù)端的的Socket服務(wù),通過ServerSocket對象;
2、服務(wù)端必須對外提供一個(gè)端口,否則客戶端無法連接;
3、獲取連接過來的客戶端對象;
4、通過客戶端對象來獲取Socket流,讀取客戶端發(fā)來的數(shù)據(jù);
5、關(guān)閉資源,關(guān)客戶端,關(guān)服務(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
|
import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { // 1、創(chuàng)建客戶端對象 ServerSocket ss = new ServerSocket( 10002 ); // 2、獲取連接過來的客戶端對象 Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); // 3、通過Socket對象獲取輸入流,讀取客戶端發(fā)來的數(shù)據(jù) InputStream in = s.getInputStream(); byte [] buf = new byte [ 1024 ]; int len = in.read(buf); String text = new String(buf, 0 , len); System.out.println(ip + ":" + text); // 4、關(guān)閉資源 s.close(); ss.close(); } } |
三、優(yōu)化TCP傳輸?shù)目蛻舳撕头?wù)端
在本部分,我們對前兩部分的內(nèi)容進(jìn)行優(yōu)化,實(shí)現(xiàn)TCP傳輸模式下的客戶端和服務(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
|
/** *優(yōu)化TCP傳輸?shù)目蛻舳?/code> */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ClientUpdate { public static void main(String[] args) throws IOException { Socket socket = new Socket( "192.168.1.100" , 10002 ); OutputStream out = socket.getOutputStream(); out.write( "tcp!" .getBytes()); // 讀取服務(wù)端返回的數(shù)據(jù),使用Socket讀取流 InputStream in = socket.getInputStream(); byte [] buf = new byte [ 1024 ]; int len = in.read(buf); String text = new String(buf, 0 , len); System.out.println(text); socket.close(); } } |
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
|
/** *優(yōu)化TCP傳輸?shù)姆?wù)端 */ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; public class ServerUpdate { public static void main(String[] args) throws IOException { // 1、創(chuàng)建服務(wù)端對象 ServerSocket ss = new ServerSocket( 10002 ); // 2、獲取連接過來的客戶端對象 Socket s = ss.accept(); //accept方式為阻塞式方法 String ip = s.getInetAddress().getHostAddress(); // 3、通過Socket對象獲取輸入流,要讀取客戶端發(fā)來的數(shù)據(jù) InputStream in = s.getInputStream(); byte [] buf = new byte [ 1024 ]; int len = in.read(buf); String text = new String(buf, 0 , len); System.out.println(ip + ":" + text); // 使用客戶端的Socket對象的輸出流給客戶端返回?cái)?shù)據(jù) OutputStream out = s.getOutputStream(); out.write( "收到" .getBytes()); s.close(); ss.close(); } } |
四、創(chuàng)建英文大寫轉(zhuǎn)換服務(wù)器
應(yīng)用TCP(Transmission Control Protocol,傳輸控制協(xié)議)的相關(guān)性質(zhì),創(chuàng)建一個(gè)基于TCP傳輸下的英文大寫轉(zhuǎn)換服務(wù)器,要求:客戶端輸入字母數(shù)據(jù),發(fā)送給服務(wù)端;服務(wù)端收到數(shù)據(jù)后顯示在控制臺(tái),并將該數(shù)據(jù)轉(zhuǎn)成大寫字母返回給客戶端;直到客戶端輸入“over”為止,轉(zhuǎn)換結(jié)束。
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
|
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class TransClient { public static void main(String[] args) throws IOException { /** * 思路:創(chuàng)建客戶端 * 1、創(chuàng)建Socket客戶端對象 * 2、獲取鍵盤錄入的數(shù)據(jù) * 3、將錄入的信息發(fā)送給Socket輸出流 * 4、讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù) */ // 1、創(chuàng)建Socket客戶端對象 Socket s = new Socket( "192.168.1.100" , 10004 ); // 2、獲取鍵盤錄入 BufferedReader bufr = new BufferedReader( new InputStreamReader(System.in)); // 3、Socket輸出流 PrintWriter out = new PrintWriter(s.getOutputStream(), true ); // 4、Socket輸入流,讀取服務(wù)端的數(shù)據(jù)并返回的大寫數(shù)據(jù) BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream())); String line = null ; while ((line = bufr.readLine()) != null ) { if ( "over" .equals(line)) break ; out.println(line); // 讀取服務(wù)端返回的一行大寫數(shù)據(jù) String upperStr = bufIn.readLine(); System.out.println(upperStr); } s.close(); } } |
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
|
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class TransServer { public static void main(String[] args) throws IOException { /** * 思路:創(chuàng)建服務(wù)端 * 1、創(chuàng)建SeverSocket客戶端對象 * 2、獲取Socket流 * 3、通過Socket, 讀取客戶端發(fā)過來的需要轉(zhuǎn)換的數(shù)據(jù) * 4、顯示在控制臺(tái)上 * 5、將數(shù)據(jù)轉(zhuǎn)換成大寫返回給客戶端 */ // 1、創(chuàng)建SeverSocket對象 ServerSocket ss = new ServerSocket( 10004 ); // 2、獲取Socket對象 Socket s = ss.accept(); // 獲取IP地址 String ip = s.getInetAddress().getHostAddress(); System.out.println(ip + "......connected" ); // 3、獲取Socket讀取流,并裝飾 BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream())); // 4、獲取Socket的輸出流,并裝飾 PrintWriter out = new PrintWriter(s.getOutputStream(), true ); String line = null ; while ((line = bufIn.readLine()) != null ) { System.out.println(line); out.println(line.toUpperCase()); } s.close(); ss.close(); } } |
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/qq_35246620/article/details/53573176