简体   繁体   English

TCP Client Server 通信 - 输入字节数组在服务器接收时被修改

[英]TCP Client Server communication - The input byte array is modified as the server receives

TCP Server: TCP服务器:

public Flux<Void> handleMessage(NettyInbound inbound, NettyOutbound outbound, boolean isSBD) {
    LOGGER.debug(LOGGER_HANDLE_MESSAGE);

    return inbound.receive().asByteArray().flatMap(bytes -> {
      LOGGER.info(LOGGER_MESSAGE_RECEIVED);
      LOGGER.debug(LOGGER_PAYLOAD, bytes);
    });
}

TCP Client: TCP客户端:

byte[] byteArray = new byte[]{(byte) 0x01, (byte) 0x12};
try (Socket socket = new Socket(host, port)) {
  ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
  objectOutputStream.writeObject(byteArray);
  ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
  return objectInputStream.readObject();
} catch (Exception ex) {
  LOGGER.error("exception occurred" + ex.getMessage());
  ex.printStackTrace();
  return "Exception";
}

When the server receives the message sent by TCP Client, I dont see the same byte array.当服务器收到TCP Client发送的消息时,我没有看到相同的字节数组。 Say, if I send byte[] byteArray = new byte[]{(byte) 0x06, (byte) 0x12};.说,如果我发送 byte[] byteArray = new byte[]{(byte) 0x06, (byte) 0x12};。 In Server, when it received, it is : [-84, -19, 0, 5, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, 112, 0, 0, 0, 2, 6, 18]在Server端,接收到的时候是:[-84, -19, 0, 5, 117, 114, 0, 2, 91, 66, -84, -13, 23, -8, 6, 8, 84, -32, 2, 0, 0, 120, 112, 0, 0, 0, 2, 6, 18]

I was trying to receive the same byte array in the server side.我试图在服务器端接收相同的字节数组。 Am I doing anything wrong while sending the byte array from client.从客户端发送字节数组时我做错了什么吗? Please advise请指教

I tried with TCPClient to send byte array rather than Socket output stream.我尝试使用 TCPClient 发送字节数组而不是 Socket 输出流。 And it worked.它奏效了。

TcpClient.create()
        .host(host)
        .port(port)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
        .wiretap(true)
        .connect()
        .flatMap(connection ->
            connection.outbound().sendByteArray(Mono.just(byteArray))
                .then(connection.inbound().receive().asByteArray().next().flatMap(bytes -> {
                  LOGGER.info("bytes {}", bytes);
                  return Mono.empty();
                }))
                .then()
        )
        .subscribe();

This sends the exact byte array to TCP Server.这会将确切的字节数组发送到 TCP 服务器。 :) :)

Abinaya阿比纳亚

Your problem is that you are sending an Object , an Array is an object as you can see on this link .您的问题是您正在发送一个Object ,一个Array是一个对象,正如您在此链接上看到的那样。 writeObject serializes the array and sent it from your client to your server and your server receive all the bytes which represents the array which is not the information that you are expecting, you are expecting only the array values. writeObject序列化数组并将其从您的客户端发送到您的服务器,您的服务器接收代表数组的所有字节,这不是您期望的信息,您只期望数组值。

Instead of using writeObject(Object obj) you should use write(byte[] buf) , which send the byte of an array.而不是使用writeObject(Object obj)你应该使用write(byte[] buf) ,它发送数组的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM