Socket一般也稱作」套接字「,用於描述IP地址和端口,廢話很少說,它就是網絡通訊過程當中端點的抽象表示。值得一提的是,Java在包java.net中提供了兩個類Socket和ServerSocket,分別用來表示雙向鏈接的客戶端和服務端。這是兩個封裝得很是好的類,使用起來很方便!java
下面將首先建立一個SocketServer的類做爲服務端以下,該服務端實現了多線程機制,能夠在特定端口處監聽多個客戶請求,一旦有客戶請求,Server老是會建立一個服務純種來服務新來的客戶,而本身繼續監聽。程序中accept()是一個阻塞函數,所謂阻塞性方法就是說該方法被調用後將等待客戶的請求,直到有一個客戶啓動並請求鏈接到相同的端口,而後accept()返回一個對應於客戶的Socket。這時,客戶方和服務方都創建了用於通訊的Socket,接下來就是由各個Socket分別打開各自的輸入、輸出流。服務器
SocketServer類,服務器實現:網絡
1 package HA.Socket;
2
3 import java.io.*;
4 import java.net.*;
5
6 public class SocketServer {
7
8 ServerSocket sever;
9
10 public SocketServer(int port){
11 try{
12 sever = new ServerSocket(port);
13 }catch(IOException e){
14 e.printStackTrace();
15 }
16 }
17
18 public void beginListen(){
19 while(true){
20 try{
21 final Socket socket = sever.accept();
22
23 new Thread(new Runnable(){
24 public void run(){
25 BufferedReader in;
26 try{
27 in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
28 PrintWriter out = new PrintWriter(socket.getOutputStream());
29 while (!socket.isClosed()){
30 String str;
31 str = in.readLine();
32 out.println("Hello!world!! " + str);
33 out.flush();
34 if (str == null || str.equals("end"))
35 break;
36 System.out.println(str);
37 }
38 socket.close();
39 }catch(IOException e){
40 e.printStackTrace();
41 }
42 }
43 }).start();
44 }catch(IOException e){
45 e.printStackTrace();
46 }
47 }
48 }
49 }多線程
SocketClient類,客戶端實現:socket
1 package HA.Socket;
2
3 import java.io.*;
4 import java.net.*;
5
6 public class SocketClient {
7 static Socket client;
8
9 public SocketClient(String site, int port){
10 try{
11 client = new Socket(site,port);
12 System.out.println("Client is created! site:"+site+" port:"+port);
13 }catch (UnknownHostException e){
14 e.printStackTrace();
15 }catch (IOException e){
16 e.printStackTrace();
17 }
18 }
19
20 public String sendMsg(String msg){
21 try{
22 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
23 PrintWriter out = new PrintWriter(client.getOutputStream());
24 out.println(msg);
25 out.flush();
26 return in.readLine();
27 }catch(IOException e){
28 e.printStackTrace();
29 }
30 return "";
31 }
32 public void closeSocket(){
33 try{
34 client.close();
35 }catch(IOException e){
36 e.printStackTrace();
37 }
38 }
39 public static void main(String[] args) throws Exception{
40
41 }
42
43 }函數
接下來就是來測試Socket通訊了!測試
先運行TestSocketServer類,打開服務端,在12345端口處監聽!spa
1 package HA.Socket;
2
3 public class TestSocketServer {
4
5 public static void main(String[] argvs){
6 SocketServer server = new SocketServer(12345);
7 server.beginListen();
8 }
9 }.net
再運行TestSocketClient類:線程
1 package HA.Socket;
2
3 public class TestSocketClient {
4
5 public static void main(String[] args){
6
7 SocketClient client = new SocketClient("127.0.0.1",12345);
8 System.out.println(client.sendMsg("nimei1"));
9 client.closeSocket();
10
11 SocketClient client1 = new SocketClient("127.0.0.1",12345);
12 System.out.println(client1.sendMsg("nimei1111"));
13 client1.closeSocket();
14
15 SocketClient client11 = new SocketClient("127.0.0.1",12345);
16 System.out.println(client11.sendMsg("nimei11111111"));
17 client11.closeSocket();
18
19 SocketClient client111 = new SocketClient("127.0.0.1",12345);
20 System.out.println(client111.sendMsg("nimei11111111111111111"));
21 client111.closeSocket();
22
23 }
24 }
輸出結果以下:
服務端:
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei1
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei1111
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei11111111
Client is created! site:127.0.0.1 port:12345
Hello!world!! nimei11111111111111111
客戶端:
nimei1nimei1111nimei11111111nimei11111111111111111