天天看點

linux作業系統中的Socket程式設計

認識Socket

socket,又稱套接字,是在不同的程序間進行網絡通訊的一種協定、約定或者說是規範。

對于socket程式設計,它更多的時候像是基于TCP/UDP等協定做的一層封裝或者說抽象,是一套系統所提供的用于進行網絡通信相關程式設計的接口。

建立socket的基本流程

我們以linux作業系統提供的基本api為例,了解建立一個socket通信的基本流程:

linux作業系統中的Socket程式設計

可以看到本質上,socket是對tcp連接配接(當然也有可能是udp等其他連接配接)協定,在程式設計層面上的簡化和抽象。

1.最基本的Socket示範

1.1 單向通信

首先,我們從隻發送和接收一次消息的socket基礎代碼開始:

服務端:

package com.marklux.socket.base;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * The very basic socket server that only listen one single message.
 */

public class BaseSocketServer {

    private ServerSocket server;
    private Socket socket;
    private int port;
    private InputStream inputStream;
    private static final int MAX_BUFFER_SIZE = 1024;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public BaseSocketServer(int port) {
        this.port = port;
    }

    public void runServerSingle() throws IOException {
        this.server = new ServerSocket(this.port);

        System.out.println("base socket server started.");
        // the code will block here till the request come.
        this.socket = server.accept();

        this.inputStream = this.socket.getInputStream();

        byte[] readBytes = new byte[MAX_BUFFER_SIZE];

        int msgLen;
        StringBuilder stringBuilder = new StringBuilder();

        while ((msgLen = inputStream.read(readBytes)) != -1) {
            stringBuilder.append(new String(readBytes,0,msgLen,"UTF-8"));
        }

        System.out.println("get message from client: " + stringBuilder);

        inputStream.close();
        socket.close();
        server.close();
    }

    public static void main(String[] args) {
        BaseSocketServer bs = new BaseSocketServer(9799);
        try {
            bs.runServerSingle();
        }catch (IOException e) {
            e.printStackTrace();
        }

    }
}
           

用戶端:

package com.marklux.socket.base;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

/**
 * The very basic socket client that only send one single message.
 */

public class BaseSocketClient {
    private String serverHost;
    private int serverPort;
    private Socket socket;
    private OutputStream outputStream;

    public BaseSocketClient(String host, int port) {
        this.serverHost = host;
        this.serverPort = port;
    }

    public void connetServer() throws IOException {
        this.socket = new Socket(this.serverHost, this.serverPort);
        this.outputStream = socket.getOutputStream();
        // why the output stream?
    }

    public void sendSingle(String message) throws IOException {
        try {
            this.outputStream.write(message.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
        this.outputStream.close();
        this.socket.close();
    }

    public static void main(String[] args) {
        BaseSocketClient bc = new BaseSocketClient("127.0.0.1",9799);
        try {
            bc.connetServer();
            bc.sendSingle("Hi from mark.");
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
           

先運作服務端,再運作用戶端,就可以看到效果。

  • 注意這裡的IO操作實作,我們使用了一個大小為MAX_BUFFER_SIZE的byte數組作為緩沖區,然後從輸入流中取出位元組放置到緩沖區,再從緩沖區中取出位元組建構到字元串中去,這在輸入流檔案很大時非常有用,事實上,後面要講到的NIO也是基于這種思路實作的。

1.2 雙向通信

上面的例子隻實作了一次單向的通信,這顯然有點浪費通道。socket連接配接支援全雙工的雙向通信(底層是tcp),下面的例子中,服務端在收到用戶端的消息後,将傳回給用戶端一個回執。

并且我們使用了一些java.io包裝好的方法,來簡化整個通信的流程(因為消息長度不大,不再使用緩沖區)。

服務端:

public void runServer() throws IOException {
        this.serverSocket = new ServerSocket(port);
        this.socket = serverSocket.accept();
        this.inputStream = socket.getInputStream();

        String message = new String(inputStream.readAllBytes(), "UTF-8");

        System.out.println("received message: " + message);

        this.socket.shutdownInput(); // 告訴用戶端接收已經完畢,之後隻能發送

        // write the receipt.

        this.outputStream = this.socket.getOutputStream();
        String receipt = "We received your message: " + message;
        outputStream.write(receipt.getBytes("UTF-8"));

        this.outputStream.close();
        this.socket.close();
    }           

用戶端:

public void sendMessage(String message) throws IOException {
        this.socket = new Socket(host,port);
        this.outputStream = socket.getOutputStream();
        this.outputStream.write(message.getBytes("UTF-8"));
        this.socket.shutdownOutput(); // 告訴伺服器,所有的發送動作已經結束,之後隻能接收
        this.inputStream = socket.getInputStream();
        String receipt = new String(inputStream.readAllBytes(), "UTF-8");
        System.out.println("got receipt: " + receipt);
        this.inputStream.close();
        this.socket.close();
    }           
  • 注意這裡我們在服務端接受到消息以及用戶端發送消息後,分别調用了shutdownInput()和shutdownOutput()而不是直接close對應的stream,這是因為在關閉任何一個stream,都會直接導緻socket的關閉,也就無法進行後面回執的發送了。
  • 但是注意,調用shutdownInput()和shutdownOutput()之後,對應的流也會被關閉,不能再次向socket發送/寫入了。

2. 發送更多的消息:結束的界定

剛才的兩個例子中,每次打開流,都隻能進行一次寫入/讀取操作,結束後對應流被關閉,就無法再次寫入/讀取了。

在這種情況下,如果要發送兩次消息,就不得不建立兩個socket,既耗資源又麻煩。其實我們完全可以不關閉對應的流,隻要分次寫入消息就可以了。

但是這樣的話,我們就必須面對另一個問題:如何判斷一次消息發送的結束呢?

2.1 使用特殊符号

最簡單的辦法是使用一些特殊的符号來标記一次發送完成,服務端隻要讀到對應的符号就可以完成一次讀取,然後進行相關的處理操作。

下面的例子中我們使用換行符\n來标記一次發送的結束,服務端每接收到一個消息,就列印一次,并且使用了Scanner來簡化操作:

服務端:

public void runServer() throws IOException {
        this.server = new ServerSocket(this.port);

        System.out.println("base socket server started.");

        this.socket = server.accept();
        // the code will block here till the request come.

        this.inputStream = this.socket.getInputStream();
        Scanner sc = new Scanner(this.inputStream);
        while (sc.hasNextLine()) {
            System.out.println("get info from client: " + sc.nextLine());
        } // 循環接收并輸出消息内容
        this.inputStream.close();
        socket.close();
    }           

用戶端:

public void connetServer() throws IOException {
        this.socket = new Socket(this.serverHost, this.serverPort);
        this.outputStream = socket.getOutputStream();
    }

public void send(String message) throws IOException {
        String sendMsg = message + "\n"; // we mark \n as a end of line.
        try {
            this.outputStream.write(sendMsg.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        }
//        this.outputStream.close();
//        this.socket.shutdownOutput();
    }

 public static void main(String[] args) {
        CycleSocketClient cc = new CycleSocketClient("127.0.0.1", 9799);
        try {
            cc.connetServer();
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String line = sc.nextLine();
                cc.send(line);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }           

運作後效果是,用戶端每輸入一行文字按下回車後,服務端就會列印出對應的消息讀取記錄。

2.2 根據長度界定

回到原點,我們之是以不好定位消息什麼時候結束,是因為我們不能夠确定每次消息的長度。

那麼其實可以先将消息的長度發送出去,當服務端知道消息的長度後,就能夠完成一次消息的接收了。

總的來說,發送一次消息變成了兩個步驟

  1. 發送消息的長度
  2. 發送消息

最後的問題就是,“發送消息的長度”這一步驟所發送的位元組量必須是固定的,否則我們仍然會陷入僵局。

一般來說,我們可以使用固定的位元組數來儲存消息的長度,比如規定前2個位元組就是消息的長度,不過這樣我們能夠傳送的消息最大長度也就被固定死了,以2個位元組為例,我們發送的消息最大長度不超過2^16個位元組即64K。

如果你了解一些字元的編碼,就會知道,其實我們可以使用變長的空間來儲存消息的長度,比如:

第一個位元組首位為0:即0XXXXXXX,表示長度就一個位元組,最大128,表示128B
第一個位元組首位為110,那麼附帶後面一個位元組表示長度:即110XXXXX 10XXXXXX,最大2048,表示2K
第一個位元組首位為1110,那麼附帶後面二個位元組表示長度:即110XXXXX 10XXXXXX 10XXXXXX,最大131072,表示128K
依次類推
複制代碼           

當然這樣實作起來會麻煩一些,是以下面的例子裡我們仍然使用固定的兩個位元組來記錄消息的長度。

服務端:

public void runServer() throws IOException {
        this.serverSocket = new ServerSocket(this.port);
        this.socket = serverSocket.accept();
        this.inputStream = socket.getInputStream();
        byte[] bytes;
        while (true) {
            // 先讀第一個位元組
            int first = inputStream.read();
            if (first == -1) {
                // 如果是-1,說明輸入流已經被關閉了,也就不需要繼續監聽了
                this.socket.close();
                break;
            }
            // 讀取第二個位元組
            int second = inputStream.read();

            int length = (first << 8) + second; // 用位運算将兩個位元組拼起來成為真正的長度

            bytes = new byte[length]; // 建構指定長度的位元組大小來儲存消息即可

            inputStream.read(bytes);

            System.out.println("receive message: " + new String(bytes,"UTF-8"));
        }
    }           

用戶端:

public void connetServer() throws IOException {
        this.socket = new Socket(host,port);
        this.outputStream = socket.getOutputStream();
    }

public void sendMessage(String message) throws IOException {
        // 首先要把message轉換成bytes以便處理
        byte[] bytes = message.getBytes("UTF-8");
        // 接下來傳輸兩個位元組的長度,依然使用移位實作
        int length = bytes.length;
        this.outputStream.write(length >> 8); // write預設一次隻傳輸一個位元組
        this.outputStream.write(length);
        // 傳輸完長度後,再正式傳送消息
        this.outputStream.write(bytes);
    }

public static void main(String[] args) {
        LengthSocketClient lc = new LengthSocketClient("127.0.0.1",9799);
        try {
            lc.connetServer();
            Scanner sc = new Scanner(System.in);
            while (sc.hasNextLine()) {
                lc.sendMessage(sc.nextLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }           

3. 處理更多的連接配接:多線程

3.1 同時實作消息的發送與接收

在考慮服務端處理多連接配接之前,我們先考慮使用多線程改造一下原有的一對一對話執行個體。

在原有的例子中,消息的接收方并不能主動地向對方發送消息,換句話說我們并沒有實作真正的互相對話,這主要是因為消息的發送和接收這兩個動作并不能同時進行,是以我們需要使用兩個線程,其中一個用于監聽鍵盤輸入并将其寫入socket,另一個則負責監聽socket并将接受到的消息顯示。

出于簡單考慮,我們直接讓主線程負責鍵盤監聽和消息發送,同時另外開啟一個線程用于拉取消息并顯示。

消息拉取線程 ListenThread.java

public class ListenThread implements Runnable {
    private Socket socket;
    private InputStream inputStream;

    public ListenThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() throws RuntimeException{
        try {
            this.inputStream = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }

        while (true) {
            try {
                int first = this.inputStream.read();
                if (first == -1) {
                    // 輸入流已經被關閉,無需繼續讀取
                    throw new RuntimeException("disconnected.");
                }
                int second = this.inputStream.read();
                int msgLength = (first<<8) + second;
                byte[] readBuffer = new byte[msgLength];
                this.inputStream.read(readBuffer);

                System.out.println("message from [" + socket.getInetAddress() + "]: " + new String(readBuffer,"UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        }
    }
}           

主線程,啟動時由使用者選擇是作為server還是client:

public class ChatSocket {
    private String host;
    private int port;
    private Socket socket;
    private ServerSocket serverSocket;
    private OutputStream outputStream;

    // 以服務端形式啟動,建立會話
    public void runAsServer(int port) throws IOException {
        this.serverSocket = new ServerSocket(port);
        System.out.println("[log] server started at port " + port);
        // 等待用戶端的加入
        this.socket = serverSocket.accept();
        System.out.println("[log] successful connected with " + socket.getInetAddress());
        // 啟動監聽線程
        Thread listenThread = new Thread(new ListenThread(this.socket));
        listenThread.start();
        waitAndSend();
    }

    // 以用戶端形式啟動,加入會話
    public void runAsClient(String host, int port) throws IOException {
        this.socket = new Socket(host, port);
        System.out.println("[log] successful connected to server " + socket.getInetAddress());
        Thread listenThread = new Thread(new ListenThread(this.socket));
        listenThread.start();
        waitAndSend();
    }

    public void waitAndSend() throws IOException {
        this.outputStream = this.socket.getOutputStream();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            this.sendMessage(sc.nextLine());
        }
    }

    public void sendMessage(String message) throws IOException {
        byte[] msgBytes = message.getBytes("UTF-8");
        int length = msgBytes.length;
        outputStream.write(length>>8);
        outputStream.write(length);
        outputStream.write(msgBytes);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ChatSocket chatSocket = new ChatSocket();
        System.out.println("select connect type: 1 for server and 2 for client");
        int type = Integer.parseInt(scanner.nextLine().toString());
        if (type == 1) {
            System.out.print("input server port: ");
            int port = scanner.nextInt();
            try {
                chatSocket.runAsServer(port);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else if (type == 2) {
            System.out.print("input server host: ");
            String host = scanner.nextLine();
            System.out.print("input server port: ");
            int port = scanner.nextInt();
            try {
                chatSocket.runAsClient(host, port);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}           

3.2 使用線程池優化服務端并發能力

作為服務端,如果一次隻跟一個用戶端建立socket連接配接,未免顯得太過浪費資源,是以我們完全可以讓服務端和多個用戶端建立多個socket。

那麼既然要處理多個連接配接,就不得不面對并發問題了(當然,你也可以寫循環輪流處理)。我們可以使用多線程來處理并發,不過線程的建立和銷毀都會消耗大量的資源和時間,是以最好一步到位,用一個線程池來實作。

下面給出一個示範性質的服務端代碼:

public class SocketServer {
  public static void main(String args[]) throws Exception {
    // 監聽指定的端口
    int port = 55533;
    ServerSocket server = new ServerSocket(port);
    // server将一直等待連接配接的到來
    System.out.println("server将一直等待連接配接的到來");

    //如果使用多線程,那就需要線程池,防止并發過高時建立過多線程耗盡資源
    ExecutorService threadPool = Executors.newFixedThreadPool(100);
    
    while (true) {
      Socket socket = server.accept();
      
      Runnable runnable=()->{
        try {
          // 建立好連接配接後,從socket中擷取輸入流,并建立緩沖區進行讀取
          InputStream inputStream = socket.getInputStream();
          byte[] bytes = new byte[1024];
          int len;
          StringBuilder sb = new StringBuilder();
          while ((len = inputStream.read(bytes)) != -1) {
            // 注意指定編碼格式,發送方和接收方一定要統一,建議使用UTF-8
            sb.append(new String(bytes, 0, len, "UTF-8"));
          }
          System.out.println("get message from client: " + sb);
          inputStream.close();
          socket.close();
        } catch (Exception e) {
          e.printStackTrace();
        }
      };
      threadPool.submit(runnable);
    }

  }
}           

4. 連接配接保活

我想你不難發現一個問題,那就是當socket連接配接成功建立後,如果中途發生異常導緻其中一方斷開連接配接,此時另一方是無法發現的,隻有在再次嘗試發送/接收消息才會因為抛出異常而退出。

簡單的說,就是我們維持的socket連接配接,是一個長連接配接,但我們沒有保證它的時效性,上一秒它可能還是可以用的,但是下一秒就不一定了。

4.1 使用心跳包

保證連接配接随時可用的最常見方法就是定時發送心跳包,來檢測連接配接是否正常。這對于實時性要求很高的服務而言,還是非常重要的(比如消息推送)。

大體的方案如下:

  1. 雙方約定好心跳包的格式,要能夠差別于普通的消息。
  2. 用戶端每隔一定時間,就向服務端發送一個心跳包
  3. 服務端每接收到心跳包時,将其抛棄
  4. 如果用戶端的某個心跳包發送失敗,就可以判斷連接配接已經斷開
  5. 如果對實時性要求很高,服務端也可以定時檢查用戶端發送心跳包的頻率,如果超過一定時間沒有發送可以認為連接配接已經斷開

4.2 斷開時重連

使用心跳包必然會增加帶寬和性能的負擔,對于普通的應用我們其實并沒有必要使用這種方案,如果消息發送時抛出了連接配接異常,直接嘗試重新連接配接就好了。

跟上面的方案對比,其實這個抛出異常的消息就充當了心跳包的角色。

總的來說,連接配接是否要保活,如何保活,需要根據具體的業務場景靈活地思考和定制

繼續閱讀