什么是Socket
網絡上的兩個程序通過一個雙向的通訊連接實現數據的交換,這個雙向鏈路的一端稱為一個Socket。Socket通常用來實現客戶方和服務方的連接。Socket是TCP/IP協議的一個十分流行的編程界面,一個Socket由一個IP地址和一個端口號唯一確定。
但是,Socket所支持的協議種類也不光TCP/IP一種,因此兩者之間是沒有必然聯系的。在Java環境下,Socket編程主要是指基于TCP/IP協議的網絡編程。
Socket通訊的過程
Server端Listen(監聽)某個端口是否有連接請求,Client端向Server 端發出Connect(連接)請求,Server端向Client端發回Accept(接受)消息。一個連接就建立起來了。Server端和Client 端都可以通過Send,Write等方法與對方通信。
對于一個功能齊全的Socket,都要包含以下基本結構,其工作過程包含以下四個基本的步驟:
(1) 創建Socket;
(2) 打開連接到Socket的輸入/出流;
?。?) 按照一定的協議對Socket進行讀/寫操作;
?。?) 關閉Socket.(在實際應用中,并未使用到顯示的close,雖然很多文章都推薦如此,不過在我的程序中,可能因為程序本身比較簡單,要求不高,所以并未造成什么影響。)
創建Socket
java在包java.net中提供了兩個類Socket和ServerSocket,分別用來表示雙向連接的客戶端和服務端。這是兩個封裝得非常好的類,使用很方便。其構造方法如下:
1
2
3
4
5
6
7
8
9
10
|
Socket(InetAddress address, int port); Socket(InetAddress address, int port, boolean stream); Socket(String host, int prot); Socket(String host, int prot, boolean stream); Socket(SocketImpl impl) Socket(String host, int port, InetAddress localAddr, int localPort) Socket(InetAddress address, int port, InetAddress localAddr, int localPort) ServerSocket(int port); ServerSocket(int port, int backlog); ServerSocket(int port, int backlog, InetAddress bindAddr) |
其中address、host和port分別是雙向連接中另一方的IP地址、主機名和端 口號,stream指明socket是流socket還是數據報socket,localPort表示本地主機的端口號,localAddr和 bindAddr是本地機器的地址(ServerSocket的主機地址),impl是socket的父類,既可以用來創建serverSocket又可 以用來創建Socket。count則表示服務端所能支持的最大連接數。例如:學習視頻網 http://www.xxspw.com
1
2
|
Socket client = new Socket( "127.0.01." , 80 ); ServerSocket server = new ServerSocket( 80 ); |
注意,在選擇端口時,必須小心。每一個端口提供一種特定的服務,只有給出正確的端口,才 能獲得相應的服務。0~1023的端口號為系統所保留,例如http服務的端口號為80,telnet服務的端口號為21,ftp服務的端口號為23, 所以我們在選擇端口號時,最好選擇一個大于1023的數以防止發生沖突。
在創建socket時如果發生錯誤,將產生IOException,在程序中必須對之作出處理。所以在創建Socket或ServerSocket是必須捕獲或拋出例外。
代碼
server
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
|
package socket; import java.io.*; import java.net.*; public class TcpServer { public static void main(String[] args) throws Exception { ServerSocket server = new ServerSocket( 9091 ); try { Socket client = server.accept(); try { BufferedReader input = new BufferedReader( new InputStreamReader(client.getInputStream())); boolean flag = true ; int count = 1 ; while (flag) { System.out.println( "客戶端要開始發騷了,這是第" + count + "次!" ); count++; String line = input.readLine(); System.out.println( "客戶端說:" + line); if (line.equals( "exit" )) { flag = false ; System.out.println( "客戶端不想玩了!" ); } else { System.out.println( "客戶端說: " + line); } } } finally { client.close(); } } finally { server.close(); } } } |
client
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
|
package socket; import java.io.*; import java.net.*; import java.util.Scanner; public class TcpClient { public static void main(String[] args) throws Exception { Socket client = new Socket( "127.0.0.1" , 9091 ); try { PrintWriter output = new PrintWriter(client.getOutputStream(), true ); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); System.out.println( "寫出了數據: " + words); } cin.close(); } finally { client.close(); } } } |
Server綁定ip
用c寫socket的時候,struct sockaddr_in 結構體是可以指定sin_addr.s_addr的,也就是可以指定ip地址,為什么會有這種需求呢,例如我的網絡鏈接是這樣的:
我可能只想綁定eth0這個網卡的ip地址,因為我的lo和wlan0都可能在用一端口做了nginx的虛擬主機,因此在服務器端開啟ServerSocket的時候,有指定ip的需求
方案
ServerSocket的一個構造函數如下:
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
參數:
port - 本地 TCP 端口
backlog - 偵聽 backlog
bindAddr - 要將服務器綁定到的 InetAddress
因為InetAddress無構造函數,我在這里糾結了好一段時間,查看stackoverflow上,可以使用InetAddress的getByName方法
示例代碼
1
2
3
|
InetAddress bindip = InetAddress.getByName( "192.168.1.168" ); ServerSocket server = new ServerSocket( 9091 , 0 , bindip); |
并發訪問
服務器端通過增加多線程來同時處理多個客戶端的請求,其實實現還是很水的,畢竟java對多線程封裝也足夠好了,我是在Server服務器端用一個內部類實現了Runnable接口,在run方法里處理客戶端的請求,將數據打印出來
server代碼
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
|
package capitalsocket; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class CapitalizeServer { private static int clientNum = 0 ; public static void main(String args[]) throws Exception { ServerSocket listener = new ServerSocket( 9898 , 0 , InetAddress.getByName( "192.168.1.168" )); try { while ( true ) { Capitalizer multip = new Capitalizer(listener.accept(), CapitalizeServer.clientNum ++); Thread t = new Thread(multip); t.start(); } } finally { listener.close(); } } private static class Capitalizer implements Runnable { private Socket client; private int id; public Capitalizer(Socket s, int id) { this .client = s; this .id = id; } public void run() { try { BufferedReader input = new BufferedReader( new InputStreamReader( this .client.getInputStream())); while ( true ) { String data = input.readLine(); if (data.equals( "bye" )) { System.out.println( "當前第" + this .id + "個客戶端度不想玩了!" ); break ; } else { System.out.println( "當前第" + this .id + "個客戶端說:" + data); } } } catch (IOException e) { e.printStackTrace(); } finally { try { this .client.close(); } catch (IOException e) { e.printStackTrace(); } } } } } |
client代碼
客戶端代碼基本沒變,增加了一個退出操作
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
|
package capitalsocket; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class CapitalizeClient { public static void main(String[] args) throws Exception { Socket client = new Socket( "192.168.1.168" , 9898 ); try { PrintWriter output = new PrintWriter(client.getOutputStream(), true ); Scanner cin = new Scanner(System.in); String words; while (cin.hasNext()) { words = cin.nextLine(); output.println(words); if (words.equals( "bye" )) { break ; } // 每寫一次數據需要sleep一會 Thread.sleep( 3000 ); } cin.close(); } finally { client.close(); } } } |