Simple socket sticky package, half package processing

package socket;

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

public class MsgServer {
    // Cache an incomplete package in a read event to be spliced ​​into a complete package when the next read event arrives
    StringBuilder StringCacheBuffer = new StringBuilder();
    boolean cache = false;

    public static void main(String[] args) {
        try {
            MsgServer server = new MsgServer();
            server.setUpServer(9292);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setUpServer(int port) throws IOException {
        ServerSocket server = new ServerSocket(port);
        while (true) {
            Socket client = server.accept();
            System.out.println("客户端IP:" + client.getRemoteSocketAddress());
            processMesage(client);
        }
    }

    private void processMesage(Socket client) throws IOException {
        InputStream ins = client.getInputStream();
        DataInputStream dins = new DataInputStream(ins);
        int bodyLen = -1;
        // server unpacking process
        byte[] data = new byte[10 ];
        byte[] cacheBuffer = null;
        byte flag = dins.readByte();
        int totalLen = dins.readInt();
        // read the length of the packet message obtained for the first time
        //try{
        int count=0;
        bodyLen = totalLen - 5;
        System.out.println("Total message length" + bodyLen);
        try{
        while (true) {
            // The header has not been read, first read the header
            if (bodyLen == -1) {
                if (bodyLen > data.length) {
                    cache = true;
                } else {
                    data = new byte[bodyLen];
                    dins.readFully(data);
                    String msgs = new String(data);
                    System.out.println("发来的内容是1:" +StringCacheBuffer.toString()+msgs);
                }
            } else {
                if (data.length >= bodyLen) {
                    byte[] datas = new byte[bodyLen];
                    dins.readFully(datas);
                    String msgs = new String(datas);
                    System.out.println("发来的内容是2:" +StringCacheBuffer.toString()+msgs);
                } else {
                    cache = true;
                }
            }
            if (cache) {
                cacheBuffer = new byte[10];
                dins.readFully(cacheBuffer);
                StringCacheBuffer.append(new String(cacheBuffer));
                count=count+10;
            }
            if(bodyLen -count<=data.length){
                bodyLen=bodyLen-count;
            }
        }
        } catch(Exception e){
            e.printStackTrace();
        }
        StringCacheBuffer=new StringBuilder();
    }
} The
code also needs to be optimized. Mainly learn custom protocols, and solve the problem of sticky package and half package.

Protocol: packet type (1 byte) + packet length (1 int) + message length

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325215441&siteId=291194637