先來一個本地的,客戶端發送請求,服務端接收請求的簡單代碼
1 package com.TCP.java;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import org.junit.Test;
10
11 //客戶端給服務端發送信息。服務端輸出此信息到控制台上
12 public class TestTCP {
13 // 客戶端
14 @Test
15 public void client() {
16 Socket socket = null;
17 OutputStream os = null;
18 try {
19 // 1.創建一個Socket的對象,通過構造器指明服務器端的IP地址,以及其接收程序的端口號
20 socket = new Socket(InetAddress.getByName("192.168.1.101"), 9090);
21 // 2.getOutputStream():發送數據,方法返回OutputStream的對象
22 os = socket.getOutputStream();
23 // 3.具體的輸出過程
24 os.write("我是客戶端".getBytes());
25 } catch (IOException e) {
26 e.printStackTrace();
27 } finally {
28 // 關閉相應的流和Socket對象
29 try {
30 if (os != null) {
31 os.close();
32 }
33 if (socket != null) {
34 socket.close();
35 }
36 } catch (IOException e) {
37 e.printStackTrace();
38 }
39 }
40 }
41
42 // 服務端
43 @Test
44 public void server() {
45 ServerSocket ss = null;
46 Socket s = null;
47 InputStream is = null;
48 try {
49 // 1.創建一個ServerSocket的對象,通過構造器指明自身的端口號
50 ss = new ServerSocket(9090);
51 // 2.調用其accept()方法,返回一個Socket對象
52 s = ss.accept();
53 // 3.調用Socket對象的getInputStream()方法,獲取一個從客戶端發送過來的輸入流
54 is = s.getInputStream();
55 // 4.對獲取的流進行操作
56 byte[] b = new byte[20];
57 int length;
58 while ((length = is.read(b)) != -1) {
59 String str = new String(b, 0, length);
60 System.out.println(str);
61 }
62 System.out.println("收到來自於" + s.getInetAddress().getHostName() + "的連接");
63 } catch (IOException e) {
64 e.printStackTrace();
65 } finally {
66 // 關閉相應的流,Socket,ServerSocket對象
67 if (is != null) {
68 try {
69 is.close();
70 } catch (IOException e) {
71 e.printStackTrace();
72 }
73 }
74 if (s != null) {
75 try {
76 s.close();
77 } catch (IOException e) {
78 e.printStackTrace();
79 }
80 }
81 if (ss != null) {
82 try {
83 ss.close();
84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87 }
88 }
89 }
90 }
再來一個稍復雜的,在服務器收到客戶端發送的請求時,給一個友好的回饋
1 package com.TCP.java;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.InetAddress;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.net.UnknownHostException;
10
11 import org.junit.Test;
12
13 //客戶端給服務端發送信息,服務端打印信息到控制台上,同時發送“已收到信息”給客戶端
14 public class TestTCP2 {
15 //客戶端
16 @Test
17 public void client(){
18 Socket s = null;
19 OutputStream os = null;
20 InputStream is = null;
21 try {
22 s = new Socket(InetAddress.getByName("192.168.1.101"),9797);
23 os = s.getOutputStream();
24 os.write("我是客戶端".getBytes());
25
26 //shutdownOutput():執行此方法,可以顯式的告訴服務端發送完畢
27 s.shutdownOutput();
28
29 is = s.getInputStream();
30 byte[] b = new byte[20];
31 int length;
32 while((length = is.read(b)) != -1){
33 String str = new String(b,0,length);
34 System.out.println(str);
35 }
36 } catch (UnknownHostException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 } catch (IOException e) {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 }
43 if(is != null){
44 try {
45 is.close();
46 } catch (IOException e) {
47 // TODO Auto-generated catch block
48 e.printStackTrace();
49 }
50 }
51 if(os != null){
52 try {
53 os.close();
54 } catch (IOException e) {
55 // TODO Auto-generated catch block
56 e.printStackTrace();
57 }
58 }
59 if(s != null){
60 try {
61 s.close();
62 } catch (IOException e) {
63 // TODO Auto-generated catch block
64 e.printStackTrace();
65 }
66 }
67 }
68 //服務端
69 @Test
70 public void server(){
71 ServerSocket ss = null;
72 Socket s = null;
73 InputStream is = null;
74 OutputStream os = null;
75 try {
76 ss = new ServerSocket(9797);
77 s = ss.accept();
78 is = s.getInputStream();
79
80 byte[] b = new byte[20];
81 int length;
82 while((length = is.read(b)) != -1){
83 String str = new String(b, 0, length);
84 System.out.println(str);
85 }
86
87 os = s.getOutputStream();
88 os.write("已收到請求".getBytes());
89 } catch (IOException e) {
90 // TODO Auto-generated catch block
91 e.printStackTrace();
92 }
93 finally{
94 if(os != null){
95 try {
96 os.close();
97 } catch (IOException e) {
98 // TODO Auto-generated catch block
99 e.printStackTrace();
100 }
101 }
102 if(is != null){
103 try {
104 is.close();
105 } catch (IOException e) {
106 // TODO Auto-generated catch block
107 e.printStackTrace();
108 }
109 }
110 if(s != null){
111 try {
112 s.close();
113 } catch (IOException e) {
114 // TODO Auto-generated catch block
115 e.printStackTrace();
116 }
117 }
118 if(ss != null){
119 try {
120 ss.close();
121 } catch (IOException e) {
122 // TODO Auto-generated catch block
123 e.printStackTrace();
124 }
125 }
126 }
127 }
128 }