1、建立TCP客戶端的Socket服務,使用的是Socket對象,建議該對象一創建就明確目的地,即要連接的主機;
2、如果連接建立成功,說明數據傳輸通道已建立,該通道就是Socket流,是底層建立好的,既然是流,說著這里既有輸入流,又有輸出流,想要輸入流或者輸出流對象,可以通過Socket來獲取,可以通過getOutputStream()和getInputStream()來獲取;
3、使用輸出流,將數據寫出;
4、關閉Socket服務。
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、創建客戶端的Socket服務 Socket socket = new Socket( "192.168.1.100" , 10002 ); // 2、獲取Socket流中輸入流 OutputStream out = socket.getOutputStream(); // 3、使用輸出流將指定的數據寫出去 out.write( "TCP is coming !" .getBytes()); // 4、關閉Socket服務 socket.close(); } } |
二、創建TCP傳輸的服務端
1、建立TCP服務端的的Socket服務,通過ServerSocket對象;
2、服務端必須對外提供一個端口,否則客戶端無法連接;
3、獲取連接過來的客戶端對象;
4、通過客戶端對象來獲取Socket流,讀取客戶端發來的數據;
5、關閉資源,關客戶端,關服務端。
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、創建客戶端對象 ServerSocket ss = new ServerSocket( 10002 ); // 2、獲取連接過來的客戶端對象 Socket s = ss.accept(); String ip = s.getInetAddress().getHostAddress(); // 3、通過Socket對象獲取輸入流,讀取客戶端發來的數據 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、關閉資源 s.close(); ss.close(); } } |
三、優化TCP傳輸的客戶端和服務端
在本部分,我們對前兩部分的內容進行優化,實現TCP傳輸模式下的客戶端和服務端的交互功能。
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
|
/** *優化TCP傳輸的客戶端 */ 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()); // 讀取服務端返回的數據,使用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
|
/** *優化TCP傳輸的服務端 */ 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、創建服務端對象 ServerSocket ss = new ServerSocket( 10002 ); // 2、獲取連接過來的客戶端對象 Socket s = ss.accept(); //accept方式為阻塞式方法 String ip = s.getInetAddress().getHostAddress(); // 3、通過Socket對象獲取輸入流,要讀取客戶端發來的數據 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對象的輸出流給客戶端返回數據 OutputStream out = s.getOutputStream(); out.write( "收到" .getBytes()); s.close(); ss.close(); } } |
四、創建英文大寫轉換服務器
應用TCP(Transmission Control Protocol,傳輸控制協議)的相關性質,創建一個基于TCP傳輸下的英文大寫轉換服務器,要求:客戶端輸入字母數據,發送給服務端;服務端收到數據后顯示在控制臺,并將該數據轉成大寫字母返回給客戶端;直到客戶端輸入“over”為止,轉換結束。
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 { /** * 思路:創建客戶端 * 1、創建Socket客戶端對象 * 2、獲取鍵盤錄入的數據 * 3、將錄入的信息發送給Socket輸出流 * 4、讀取服務端的數據并返回的大寫數據 */ // 1、創建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輸入流,讀取服務端的數據并返回的大寫數據 BufferedReader bufIn = new BufferedReader( new InputStreamReader(s.getInputStream())); String line = null ; while ((line = bufr.readLine()) != null ) { if ( "over" .equals(line)) break ; out.println(line); // 讀取服務端返回的一行大寫數據 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 { /** * 思路:創建服務端 * 1、創建SeverSocket客戶端對象 * 2、獲取Socket流 * 3、通過Socket, 讀取客戶端發過來的需要轉換的數據 * 4、顯示在控制臺上 * 5、將數據轉換成大寫返回給客戶端 */ // 1、創建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