NioEventLoopGroup worker=new NioEventLoopGroup ();
The bootstrap. Group (worker);
The bootstrap. Channel (NioSocketChannel. Class);
The bootstrap. Handler (new ChannelInitializer() {
@ Override
Protected void initChannel Channel (Channel) throws the Exception {
//big end send order
Channel. Pipeline (.) addLast (new LengthFieldBasedFrameDecoder (ByteOrder BIG_ENDIAN, 1024 * 1024,2,2, 4, 0, true));
Channel. Pipeline (.) addLast (new ClientHandler ());
}
});
ChannelFuture f=the bootstrap. Connect (" 127.0.0.1 ", 3000);
//send a message
Byte [] MSG="I am a Java netty client MSG". GetBytes ();
//if the MSG is a simple string, go to the acceptable if into a byte array, go just a block to the conn. Read (buffer)
F. hannel (). WriteAndFlush (MSG)
I golang TCP server writing either
Func main () {
Listen, err:=net. Listen (" TCP ", "127.0.0.1:30000")
if err ! Nil={
FMT. Println (" listen failed, err: "to err)
Return
}
Defer listen. The Close ()
For {
The conn, err: listen.=the Accept ()
if err ! Nil={
FMT. Println (" accept failed, err: "to err)
The continue
}
Go func () {
Defer conn. Close ()
Var num int32
Buffer:=make byte [], (6)
_, err:=conn. Read (buffer)//if the Java netty hair of byte [] an array of bytes, has been blocked in this line
if err ! Nil={
Return
}
Buf:=bytes. NewReader (buffer)
Err=binary. Read (buf, binary BigEndian, & amp; Num)
FMT. Println (buf)
} ()
}
}
CodePudding user response:
Golang listening port is 30000, netty connection port is 3000, port is wrong, to the same port,