Home > Back-end >  Netty socket one of the most simple example code, trouble can't see why the server monitoring n
Netty socket one of the most simple example code, trouble can't see why the server monitoring n

Time:09-18

The following socket server is the most simple example, with the realization of netty
Is written in the native Java socket client;
The client after the connection is established MyServerHandler channelActive can listen to, but why didn't channelRead0 method has been listening to the data?
Under the great god, please help to see where the problem, the solution to the points!

 

The import io.net ty. The bootstrap. ServerBootstrap;
The import io.net ty. Channel. *;
The import io.net ty. Channel. Nio. NioEventLoopGroup;
The import io.net ty. Channel. Socket. A SocketChannel;
The import io.net ty. Channel. The socket. Nio. NioServerSocketChannel;
The import io.net ty. Handler. Codec. LengthFieldBasedFrameDecoder;
The import io.net ty. Handler. Codec. LengthFieldPrepender;
The import io.net ty. Handler. Codec. String. StringDecoder;
The import io.net ty. Handler. Codec. String. StringEncoder;
The import io.net ty. Util. CharsetUtil;

/* *
* @ author
*/
Public final class ServerSimplest {

Public static void main (String [] args) {
EventLoopGroup bossGroup=new NioEventLoopGroup ();
EventLoopGroup workGroup=new NioEventLoopGroup ();

Try {
ServerBootstrap ServerBootstrap=new ServerBootstrap ();
ServerBootstrap. Group (bossGroup, workGroup). Channel (NioServerSocketChannel. Class).
ChildHandler (new ChannelInitializer () {
@ Override
Public void initChannel (SocketChannel SocketChannel) {
System. The out. Println (" initChannel: "+ socketChannel);
ChannelPipeline pipeline=socketChannel. Pipeline ();
Pipeline. AddLast (new LengthFieldBasedFrameDecoder (Integer. MAX_VALUE, 0,4,0,4));
Pipeline. AddLast (new LengthFieldPrepender (4));
Pipeline. AddLast (new StringDecoder (CharsetUtil UTF_8));
Pipeline. AddLast (new StringEncoder (CharsetUtil UTF_8));
Pipeline. AddLast (new MyServerHandler ());
}
});

ChannelFuture ChannelFuture=serverBootstrap. Bind (8888). The sync ();
ChannelFuture. Channel (.) closeFuture (). The sync ();
} the catch (Exception e) {
e.printStackTrace();
} the finally {
BossGroup. ShutdownGracefully ();
WorkGroup. ShutdownGracefully ();
}
}
}
The class MyServerHandler extends SimpleChannelInboundHandler {
@ Override
Protected void channelRead0 (ChannelHandlerContext CTX, String s) throws the Exception {
System. The out. Println (" the service side received "+ CTX. Channel () remoteAddress () +", "+ s);
CTX. WriteAndFlush (" server receives "+ s);
}
@ Override
Public void channelActive ChannelHandlerContext (CTX) throws the Exception {
System. The out. Println (" greetings from the service ");
CTX. WriteAndFlush (" greetings from the service \ n ");
}
@ Override
Public void exceptionCaught (ChannelHandlerContext CTX, Throwable cause) throws the Exception {
Cause the printStackTrace ();
CTX. Close ();
}
}


Java native client (no reference netty)
 
Import the Java. IO. BufferedReader;
import java.io.IOException;
Import the Java. IO. InputStream;
Import the Java. IO. InputStreamReader;
The import java.net.Socket;


Public class Client {
Private static final String HOST="127.0.0.1";
Private static final int PORT=8888;

Public static void main (String [] args) throws IOException {
Final Socket Socket=new Socket (HOST, PORT);

New Thread (new Runnable () {
@ Override
Public void the run () {
System. Out.println (" the client startup success!" );
While (true) {
Try {
String message="hello world";
System. The out. Println (" the client sends data: "+ message);
Socket. GetOutputStream (). The write (message. GetBytes ());

} the catch (Exception e) {
System. Out.println (" write data error!" );
}
Try {
Thread.sleep(5000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}


}
}). The start ();

//receive the server response
InputStream InputStream=socket. GetInputStream ();
BufferedReader br=new BufferedReader (new InputStreamReader (inputStream));
String info=null;
While ((info=br. ReadLine ())!=null) {
System. Out.println (" to receive the response from the server!" + info);
}

}
}


After the operation
The service side print:
InitChannel: [id: 0 xd0305a18, L:/127.0.0.1:8888 - R:/127.0.0.1:12462]
Greetings from the service

The client printing:
The client startup success!
The client sends data: hello world
Receive the response from the server! Greetings from the service
The client sends data: hello world
The client sends data: hello world
The client sends data: hello world
The client sends data: hello world



And if I use this Java native server (no reference netty) is to be able to listen to the helloworld
 
import java.io.IOException;
Import the Java. IO. InputStream;
Import the Java. IO. OutputStreamWriter;
Import the Java. IO. PrintWriter;
The import java.net.ServerSocket;
The import java.net.Socket;


Public class ServerBoot {

Private static final int PORT=8888;

Public static void main (String [] args) {
Server Server=new Server (PORT);
Server. The start ();
}

}
The class Server {

Private ServerSocket ServerSocket.

Public Server (int port) {
Try {
Enclosing serverSocket=new serverSocket (port);
System. The out. Println (" server startup success, port: "+ port);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related