记录一个如何解决java与C++socket通信的大小端问题

问题背景

oracle jdk默认的socket通信发送int类型数据高位优先。下面是jdk包内部相关源码。(模拟)

os.write((len >>> 24) & 0xFF);
os.write((len >>> 16) & 0xFF);
os.write((len >>> 8) & 0xFF);
os.write((len >>> 0) & 0xFF);

可以很明显的看出写入流时,先右移了24位。因为int类型的数据在jdk中是以4个字节表示的。1个字节有拥有8位。这是如果按照这个顺序与C++通信会发生误读情况,转成10进制以后,数字完全变了。故而要调整jdk源码。如下方法所示:

 /**
     * 字节序转换发送到server
     * 针对发送int类型数据
     */
    public void intTrans(int length, OutputStream os) throws IOException {
        os.write((length >>> 0) & 0xFF);
        os.write((length >>> 8) & 0xFF);
        os.write((length >>> 16) & 0xFF);
        os.write((length >>> 24) & 0xFF);
    }

其他整型可类比。long型是8字节。

当然,同时做socket通信时,服务端接收时,协议所定义的包头大小很显然也是要做同样的大小端问题处理。原理一样。下面贴出了我的服务端代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Jerry
 * @date 2018/11/6
 */
@Component
@Order(value = 1)
public class SocketServer implements ApplicationRunner {

    private static Logger logger = LoggerFactory.getLogger(SocketServer.class);

    /**
     * 取到客户端发来的int类型的消息头,并且解决JDK大小端问题
     * @param b
     * @return
     */
    private static int byteArrayToInt(byte[] b){
        return b[0]&0xFF | (b[1]&0xFF) << 8 | (b[2]&0xFF) << 16 | (b[3]&0xFF) << 24;
    }

    @Value("${pixelMaster.listen.port}")
    private int port;
    public void startSocketServer() throws IOException {

        ServerSocket serverSocket = new ServerSocket(port);
        while (true){
            Socket client = serverSocket.accept();
            new HandlerThread(client);
        }
    }

    private class HandlerThread implements Runnable{

        private Socket socket;
        private HandlerThread(Socket client){
            socket = client;
            new Thread(this).start();
        }

        @Override
        public void run() {
            try{
                InputStream input = socket.getInputStream();
                byte[] datalen = new byte[4];
                input.read(datalen);
                int length = byteArrayToInt(datalen);
                logger.info("客户端发来的消息长度是:"+length);
                byte[] data = new byte[length];
                input.read(data);
                String recvMsg = new String(data);//将获得数据转为字符串类型
                logger.info("客户端发来的信息是:"+recvMsg);
                input.close();
                }catch (Exception e){
                e.printStackTrace();
                logger.error("获取客户端信息异常");
                }
            }
        }

    @Override
    public void run(ApplicationArguments args) throws IOException {
        startSocketServer();
    }
}

顺便也贴出客户端代码留作记录。

import com.mcwl.pixelmaster.utils.ByteOrderTransAndSend;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @author Jerry
 * @date 2018/11/1
 * 描述:socket连接客户端
 */
@Component
public class Client {

    //服务器地址
    @Value(value = "${pixelMaster.server.url}")
    private String ipAddr;

    //服务器端口
    @Value(value = "${pixelMaster.server.port}")
    private int port;

    public void send(String message)throws IOException{

        Socket socket = new Socket(ipAddr,port);
        OutputStream os = socket.getOutputStream();
        ByteOrderTransAndSend trans = new ByteOrderTransAndSend();
        trans.intTrans(message.getBytes().length,os);
        PrintWriter writer = new PrintWriter(os);
        writer.write(message);
        writer.flush();
        socket.shutdownOutput();
    }
}

你可能感兴趣的:(记录一个如何解决java与C++socket通信的大小端问题)