Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 网络粘包,通信中的拆包问题_Java_Sockets_Tcp_Netty_Pack - Fatal编程技术网

Java 网络粘包,通信中的拆包问题

Java 网络粘包,通信中的拆包问题,java,sockets,tcp,netty,pack,Java,Sockets,Tcp,Netty,Pack,我在节目中遇到了netty pack问题。我的程序是这样的: protected void encode(ChannelHandlerContext channelHandlerContext, OriginMessageEntity message, ByteBuf byteBuf){ try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); if(

我在节目中遇到了netty pack问题。我的程序是这样的:

        protected void encode(ChannelHandlerContext channelHandlerContext, OriginMessageEntity message, ByteBuf byteBuf){

    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        if(message.getProtocolVersion()!=null)
            outputStream.write(message.getProtocolVersion());
        else outputStream.write(PrintConstants.PROTOCOL_HEADER_PROTOCOL_NUMBER);

        if(message.getByteLength()!=null)
            outputStream.write(message.getByteLength());
        else outputStream.write(PrintConstants.MESSAGE_DEFAULT_BYTELENGTH);

        if(message.getCmdType()!=null)
            outputStream.write(message.getCmdType());
        else outputStream.write(PrintConstants.MESSAGE_DEFAULT_CMDTYPE);

        if (message.getData() != null) outputStream.write(message.getData());

        byteBuf.writeBytes(outputStream.toByteArray());

    } catch (Exception e) {
        logger.error("error。", e);
    }
}
我们使用:协议版本,数据长度,命令类型,数据。 数据长度字段记录数据字段的长度。 大多数情况下,程序运行良好。但有时,数据长度字段不等于数据字段的长度。比如:长度字段:400,实际数据长度:398

有什么问题吗?netty是否能够在不丢失数据或添加数据的情况下控制数据包