TCP在Java中的一般应用

java里的TCP有服务端+客户端一说,网上搜索Server+client,那是一打一打的,内容都大同小异。但是这个一般是对于短连接来说的,但是在实际工作中,服务端和客户端保持长连接还是比较常见的方式,这样可以减少短连接在创建连接时所消耗的时间,对提高服务器性能起到很大的提升,但是长连接中,因为多个数据的传输使用同一个通道,所以存在一个定长解析的过程(不知道是不是这么叫法)。

看图说话:
这里写图片描述

client和server之间的传输方式无须多言,是双向的。有接收就有发送。
主要要说的是所传输的数据结构。一般来说,数据都是通过字节流(可以理解为byte[])的方式来体现,实际运用中,其有2部分,一是header,一是body。
header:一般是一个4个字节的定长数组,代表的是body的字节长度。
body:代表数据本身。

当client–>server发送数据时,client先封装字节数组,包括header和body。当server接收到数据时,先读取数据流前4个字节,转换成整数(int类型占4个字节)即是body数据的字节长度,然后再继续向后读body字节长度的数据。这样就完成一次数据交互。

而server–>client响应数据,也是同样的原理。这里不再解析。

相关Java代码:

/**
 * Created by claireliu on 2017/5/6.
 */
public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(9999);

        while (true) {
            Socket socket = null;
            InputStream in = null;
            OutputStream out = null;
            try {
                System.out.println("初始化。。。");
                socket = serverSocket.accept();// 从连接队列中取出一个连接,如果没有则等待

                System.out.println("收到请求。。。");

                in = socket.getInputStream();

                byte[] headerBuf = new byte[4];

                in.read(headerBuf);

                int bodyLength = TypeUtil.bytesToInt(headerBuf, 0);

                System.out.println("bodyLength:" + bodyLength);
                byte[] bodyBuf = new byte[bodyLength];
                in.read(bodyBuf);

                System.out.println("client said:" + new String(bodyBuf));

                out = socket.getOutputStream();

                String response = "Ok";

                byte[] responseHeaderBuf = TypeUtil.int2Bytes(response.getBytes().length);

                byte[] responseBodyBuf = response.getBytes();
                out.write(responseHeaderBuf);
                out.write(responseBodyBuf);

                out.flush();


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(in != null) {
                    try {
                        in.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if(out != null) {
                    try {
                        out.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
                if(socket != null){
                    try {
                        socket.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
    }
}
public class Client {

    public static void main(String[] args) {

        InputStream in = null;
        OutputStream out = null;
        Socket socket = null;
        try{
            socket = new Socket("localhost",9999);
            out = socket.getOutputStream();

            String response = "xxx";

            System.out.println("reponse:" + response.getBytes().length);

            byte[] responseHeaderBuf = TypeUtil.int2Bytes(response.getBytes().length);

            byte[] responseBodyBuf = response.getBytes();
            out.write(responseHeaderBuf);
            out.write(responseBodyBuf);

            out.flush();

            in = socket.getInputStream();
            // 读头信息,即Body长度
            byte[] headerBuf = new byte[4];
            in.read(headerBuf);

            int bodyLength = TypeUtil.bytesToInt(headerBuf, 0);

            System.out.println("bodyLenth.." + bodyLength);
            byte[] bodyBuf = new byte[bodyLength];
            in.read(bodyBuf);

            // 输出

            System.out.println("server said:" + (new String(bodyBuf)));


        }catch (Exception e){
            e.printStackTrace();

        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

}
public class TypeUtil {


    /**
     * int返回字节数组
     * 
     * @param num
     * @return
     */
    public static byte[] int2Bytes(int num) {
        byte[] byteNum = new byte[4];
        for (int ix = 0; ix < 4; ++ix) {
            int offset = 32 - (ix + 1) * 8;
            byteNum[ix] = (byte) ((num >> offset) & 0xff);
        }
        return byteNum;
    }


    /**
     * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。
     */
    public static int bytesToInt(byte[] src, int offset) {
        int value;
        value = (int) (((src[offset] & 0xFF) << 24) | ((src[offset + 1] & 0xFF) << 16) | ((src[offset + 2] & 0xFF) << 8)
                | (src[offset + 3] & 0xFF));
        return value;
    }


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章