**我的java netty client写法如下**
<code>
```
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", 30000);
//发送消息
byte[] msg = "I am java netty client msg".getBytes();
//如果msg为单纯的字符串,go可以接受到,如果改成byte数组,go就一阻塞到 conn.Read(buffer)
f.channel().writeAndFlush(msg)
```
</code>
**我的golang tcp server 写法如下**
<code>
```
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:30000")
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)
}()
}
}
```
</code>
如果java netty 发的byte[] 字节数组的话 ,就一直阻塞 _, err := conn.Read(buffer)
**请问是怎么回事,大小端问题?**
```
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:30000")
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()
buffer := make([]byte, 6)
reader := bufio.NewReader(conn) //改成这样还是一样的 阻塞了
read, _ := reader.Read(buffer)
fmt.Println(read)
fmt.Println(reader)
}()
}
}
```
#4
更多评论