Practical netty (1) An rdate server instance based on netty
- Author: Zhong Chao (poechant)
- Email: zhongchao. USTC # gmail.com
- Blog: blog.csdn.net/poechant
- Weibo: weibo.com/lauginhom
Netty is an efficient Java Network Framework. For a brief introduction, see Introduction and comparison of Java NiO framework Mina, netty, and grizzly. For Java developers who are not familiar with NiO, it takes a short time to familiarize themselves with netty. Of course, netty has better encapsulated NiO, so you don't have to finish understanding NiO first, and then learn about netty.
The following is a simple rdate server instance. For most of the Code, see netty official examples. What is rdate? If you are a * nix user, you must know:
rdate is a command to set the system's date from a remote host.
The details are as follows:
rdate displays and sets the local date and time from the host name oraddress given as the argument. The time source may be an RFC 868 TCPprotocol server, which is usually implemented as a built-in service ofinetd(8), or an RFC 2030 protocol SNTP/NTP server. By default, rdateuses the RFC 868 TCP protocol.
Rdateserver. Java
package com.sinosuperman.test.netty;import java.net.InetSocketAddress;import java.util.concurrent.Executors;import org.jboss.netty.bootstrap.ServerBootstrap;import org.jboss.netty.channel.ChannelFactory;import org.jboss.netty.channel.ChannelPipeline;import org.jboss.netty.channel.ChannelPipelineFactory;import org.jboss.netty.channel.Channels;import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;public class RdateServer { private static int port = 28080; public static void main(String[] args) { // ChannelFactory is a factory which creates and manages Channels and // its related resources. ChannelFactory factory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), // boss executor Executors.newCachedThreadPool()); // worker executor // ServerBootstrap is a helper class that sets up a server. You can set // up the server using a Channel directly. However, please note that // this is a tedious process and you do not need to do that in most // cases. ServerBootstrap bootstrap = new ServerBootstrap(factory); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(new RdateServerHandler()); } }); bootstrap.setOption("child.tcpNoDelay", true); bootstrap.setOption("child.keepAlive", true); bootstrap.bind(new InetSocketAddress(port)); }}
Rdateserverhandler. Java
The following is an interesting channelbuffer similar to bytebuffer in NIO, which is very useful. Channelfuture is also frequently used in netty.
package com.sinosuperman.test.netty;import org.jboss.netty.buffer.ChannelBuffer;import org.jboss.netty.buffer.ChannelBuffers;import org.jboss.netty.channel.Channel;import org.jboss.netty.channel.ChannelFuture;import org.jboss.netty.channel.ChannelFutureListener;import org.jboss.netty.channel.ChannelHandlerContext;import org.jboss.netty.channel.ChannelStateEvent;import org.jboss.netty.channel.ExceptionEvent;import org.jboss.netty.channel.SimpleChannelHandler;public class RdateServerHandler extends SimpleChannelHandler { @Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { Channel ch = e.getChannel(); // To send a new message, we need to allocate a new buffer which will // contain the message. We are going to write a 32-bit integer, and // therefore we need a ChannelBuffer whose capacity is 4 bytes. The // ChannelBuffers helper class is used to allocate a new buffer. Besides // the buffer method, ChannelBuffers provides a lot of useful methods // related to the ChannelBuffer. For more information, please refer to // the API reference. ChannelBuffer time = ChannelBuffers.buffer(4); time.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L)); ChannelFuture f = ch.write(time); f.addListener(ChannelFutureListener.CLOSE); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { e.getCause().printStackTrace(); Channel ch = e.getChannel(); ch.close(); }}
Here, it is important to calculate the time formula:
System.currentTimeMillis() / 1000L + 2208988800L
I will not elaborate on it. It is a little common sense.
Client Test
The client is the rdate Command provided by Linux, Which is tested as follows:
$ rdate -o <rdate_server_port> -p <rdate_server_ip>
28080 is used by default.
$rdate -o 28080 -p 127.0.0.1Fri Dec 28 14:21:05 CST 2012
-
Reprinted please indicate from Zhong Chao (poechant) csdn blog: http://blog.csdn.net/poechant, author microblogging: http://weibo.com/lauginhom
-