function to implement: client sends the request, server side accepts the request, returns the current time, the client side prints the current time.
A:server End server
Package Com.xiaobing.netty.third;
Import Io.netty.bootstrap.ServerBootstrap;
Import Io.netty.channel.ChannelFuture;
Import Io.netty.channel.ChannelInitializer;
Import io.netty.channel.ChannelOption;
Import Io.netty.channel.EventLoopGroup;
Import Io.netty.channel.nio.NioEventLoopGroup;
Import Io.netty.channel.socket.SocketChannel;
Import Io.netty.channel.socket.nio.NioServerSocketChannel;
public class Timeserver {
public void bind (int port) throws exception{
The server accepts the client's connection thread
Eventloopgroup Bossgroup = new Nioeventloopgroup ();
Network read-write thread
Eventloopgroup Workergroup = new Nioeventloopgroup ();
Server-side Secondary startup class
Serverbootstrap B = new Serverbootstrap ();
try {
B.group (Bossgroup, Workergroup). Channel (Nioserversocketchannel.class)
. option (Channeloption.so_backlog, 1024)
. Childhandler (new Childchannelhandler ());
Blocking waits for port bindings to complete
Channelfuture f = b.bind (port). sync ();
Waiting for service-side link to close
F.channel (). Closefuture (). sync ();
}finally{
Bossgroup.shutdowngracefully ();
Workergroup.shutdowngracefully ();
}
}
Network IO Processing class
Private class Childchannelhandler extends channelinitializer<socketchannel>{
@Override
protected void Initchannel (Socketchannel arg0) throws Exception {
Arg0.pipeline (). AddLast (new Timeserverhandler ());
}
}
public static void Main (string[] args) throws Exception {
int port = 8080;
New Timeserver (). bind (port);
}
}
B:server End Processing Class
Package Com.xiaobing.netty.third;
Import Io.netty.buffer.ByteBuf;
Import io.netty.buffer.Unpooled;
Import Io.netty.channel.ChannelHandlerAdapter;
Import Io.netty.channel.ChannelHandlerContext;
Read and Write network events
public class Timeserverhandler extends Channelhandleradapter {
@Override
public void Channelread (Channelhandlercontext ctx, Object msg) throws exception{
Bytebuf buf = (bytebuf) msg;
Gets the number of buffer readable bytes
byte[] req = new byte[buf.readablebytes ()];
Buf.readbytes (req);
String BODY = new String (req, "UTF-8");
System.out.println ("The time server receive order:" + body);
String currenttimestring = "QUERY time ORDER". Equalsignorecase (body), New Java.util.Date (System.currenttimemillis ()). ToString (): "Bad ORDER";
Reply message
Bytebuf resp = Unpooled.copiedbuffer (Currenttimestring.getbytes ());
Ctx.write (RESP);
}
@Override
public void Channelreadcomplete (Channelhandlercontext ctx) {
Writes messages from the message send queue to Socketchannel
Ctx.flush ();
}
@Override
public void Exceptioncaught (Channelhandlercontext ctx,throwable cause) {
When an exception occurs, close CTX
Ctx.close ();
}
}
C: Client Request
Package Com.xiaobing.netty.third;
Import Java.security.acl.Group;
Import Io.netty.bootstrap.Bootstrap;
Import Io.netty.channel.ChannelFuture;
Import Io.netty.channel.ChannelInitializer;
Import io.netty.channel.ChannelOption;
Import Io.netty.channel.EventLoopGroup;
Import Io.netty.channel.nio.NioEventLoopGroup;
Import Io.netty.channel.socket.SocketChannel;
Import Io.netty.channel.socket.nio.NioSocketChannel;
public class Timeclient {
public void Connect (int port, String host) throws exception{
Eventloopgroup Group = new Nioeventloopgroup ();
try{
Client-initiated helper classes
Bootstrap B = new Bootstrap ();
B.group (Group). Channel (Niosocketchannel.class)
. option (Channeloption.tcp_nodelay, True)
. Handler (new channelinitializer<socketchannel> () {
Setting Channelhandler to Channelpipeline at initialization time
@Override
protected void Initchannel (Socketchannel ch) throws Exception {
Ch.pipeline (). AddLast (new Timeclienthandler ());
}
});
Initiating an asynchronous connection
Channelfuture f = b.connect (Host,port). sync ();
Waiting for client link to close
F.channel (). Closefuture (). sync ();
}finally{
Group.shutdowngracefully ();
}
}
public static void Main (string[] args) throws Exception {
int port = 8080;
New Timeclient (). Connect (port, "127.0.0.1");
}
}
D client handling of server-side return values
Package Com.xiaobing.netty.third;
Import Io.netty.buffer.ByteBuf;
Import io.netty.buffer.Unpooled;
Import Io.netty.channel.ChannelHandlerAdapter;
Import Io.netty.channel.ChannelHandlerContext;
Read and Write network events
public class Timeserverhandler extends Channelhandleradapter {
@Override
public void Channelread (Channelhandlercontext ctx, Object msg) throws exception{
Bytebuf buf = (bytebuf) msg;
Gets the number of buffer readable bytes
byte[] req = new byte[buf.readablebytes ()];
Buf.readbytes (req);
String BODY = new String (req, "UTF-8");
System.out.println ("The time server receive order:" + body);
String currenttimestring = "QUERY time ORDER". Equalsignorecase (body), New Java.util.Date (System.currenttimemillis ()). ToString (): "Bad ORDER";
Reply message
Bytebuf resp = Unpooled.copiedbuffer (Currenttimestring.getbytes ());
Ctx.write (RESP);
}
@Override
public void Channelreadcomplete (Channelhandlercontext ctx) {
Writes messages from the message send queue to Socketchannel
Ctx.flush ();
}
@Override
public void Exceptioncaught (Channelhandlercontext ctx,throwable cause) {
When an exception occurs, close CTX
Ctx.close ();
}
}
Netty Authoritative Guide---------Chapter III Introductory Application