为什么用java netty client 发送byte数组 到 golang tcp server 接收不到呢?

我的java netty client写法如下

NioEventLoopGroup worker = new NioEventLoopGroup();   
        bootstrap.group(worker);   
        bootstrap.channel(NioSocketChannel.class);   
        bootstrap.handler(new ChannelInitializer<Channel>() {   
            @Override   
            protected void initChannel(Channel channel) throws Exception {   
                //大端发送顺序   
                channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(ByteOrder.BIG_ENDIAN,1024*1024,2,2,-4,0,true));    
                channel.pipeline().addLast(new ClientHandler());   
            }  
        });    
            ChannelFuture f = bootstrap.connect("127.0.0.1", 3000);     
            //发送消息  
            byte[] msg = "I am java netty client msg".getBytes();  
            //如果msg为单纯的字符串,go可以接受到,如果改成byte数组,go就一阻塞到  conn.Read(buffer)
            f.channel().writeAndFlush(msg)

我的golang tcp server 写法如下

  func main() {
    listen, err := net.Listen("tcp", "127.0.0.1:3000")
    if err != nil {
        fmt.Println("listen failed, err:", err)
        return
    }
    defer listen.Close()
    for {
        conn, err := listen.Accept()
        if err != nil {
            fmt.Println("accept failed, err:", err)
            continue
        }
        go func() {
            defer conn.Close()

            var num int32
            buffer := make([]byte, 6)
            _, err := conn.Read(buffer)  //如果java netty 发的byte[] 字节数组的话,则一直阻塞在这一行
            if err != nil {
                return
            }
            buf := bytes.NewReader(buffer)
            err = binary.Read(buf, binary.BigEndian, &num)
            fmt.Println(buf)
        }()
    }
} 

如果java netty 发的byte[] 字节数组的话 ,就一直阻塞 _, err := conn.Read(buffer)

请问是怎么回事,大小端问题?

共 0 个回复