Practical Netty (1) 基于Netty实现的一个rdate server实例
Netty 是一个高效的 Java 网络框架,简单的介绍可参见《Java NIO框架Mina、Netty、Grizzly介绍与对比》一文。对于不熟悉 NIO 的 Java 开发者,Netty 的使用需要一小段的时间来熟悉。当然 Netty 已经更好地封装了 NIO,所以您大可不必先去了解完 NIO,再来了解 Netty。
下面是一个简单的 rdate server 实例,大部分代码参考 Netty Official Examples。什么是 rdate?如果你是 *nix 用户的话,就一定知道:
rdate is a command to set the system's date from a remote host.
具体如下:
rdate displays and sets the local date and time from the host name or
address given as the argument. The time source may be an RFC 868 TCP
protocol server, which is usually implemented as a built-in service of
inetd(8), or an RFC 2030 protocol SNTP/NTP server. By default, rdate
uses 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
下面比较有意思的就是和 NIO 中的 ByteBuffer 类似的 ChannelBuffer,很好用。ChannelFuture 也是 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();
}
}
这里比较重要的是时间的计算公式:
System.currentTimeMillis() / 1000L + 2208988800L
具体不细说了,算是一个小常识吧。
客户端测试
客户端就是 Linux 提供的 rdate 命令,如下方式测试:
$ rdate -o <rdate_server_port> -p <rdate_server_ip>
这里默认使用的是 28080。
$rdate -o 28080 -p 127.0.0.1
Fri Dec 28 14:21:05 CST 2012
-
转载请注明来自钟超(Poechant)的 CSDN 博客:http://blog.csdn.net/poechant,作者微博:http://weibo.com/lauginhom
-
分享到:
相关推荐
利用netty实现Modbus TCP client/server READ COILS | 0x01 READ DISCRETE INPUTS | 0x02 READ HOLDING REGISTERS | 0x03 READ INPUT REGISTERS | 0x04 WRITE SINGLE COIL | 0x05 WRITE SINGLE REGISTER | 0x06 ...
netty案例,netty4.1中级拓展篇十三《Netty基于SSL实现信息传输过程中双向加密验证》源码 ...
JAVA采用Netty库实现基于以DTU传输的TCP服务器 ,可以支持多端口通讯 ,同时也支持 多协议解析
也就是说,Netty 是一个基于NIO的客户,服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户,服务端应用。Netty相当简化和流线化了网络应用的编程开发过程,例如,...
一个基于netty实现web框架,或者mvc框架,实现基于netty的web框架,你说netty强不强,文中有不对的地方,欢迎大牛指正
基于springboot+netty+redis+hdfs实现的一个线上分布式网盘系统,毕业设计项目,个人学习,课设等均可,项目经过测试,可完美运行! 项目结构 项目主要分为一下几个模块 web-service 主要功能是页面展示,交互 ...
基于Netty实现的内网穿透&反向代理的工具 (支持TCP上层协议和HTTP的穿透式反向代理).zip
基于Netty实现的命令行斗地主游戏,为划水摸鱼而生~ 基于Netty实现的命令行斗地主游戏,为划水摸鱼而生~ 基于Netty实现的命令行斗地主游戏,为划水摸鱼而生~ 基于Netty实现的命令行斗地主游戏,为划水摸鱼而生...
netty实现websocket server。通过构建java客户端程序满足页面的发起websocket请求,实现业务的交互
基于 Java Netty实现的可用于内网穿透的代理工具.zip基于 Java Netty实现的可用于内网穿透的代理工具.zip基于 Java Netty实现的可用于内网穿透的代理工具.zip基于 Java Netty实现的可用于内网穿透的代理工具.zip基于...
0、maven项目 1、需要手动加入依赖RXTXcomm.jar 2、将rxtxParallel.dll,rxtxSerial.dll放到<JAVA_HOME>\jre\bin下 3、运行App.java主程序
Netty学习笔记_Springboot实现自定义协议.docx Netty学习笔记_Springboot实现自定义协议.docx Netty学习笔记_Springboot实现自定义协议.docx
使用Netty分别实现了三个Socket server和一个socket client: > * server1:9099 主要用来跟硬件传感器通信 > * server2:8888/websocket 作为websocket服务端跟网页通信 > * server2:8889/websocket 跟storm服务做...
Android基于Netty框架实现通信
JAVA版基于netty的物联网高并发智能网关 JAVA版基于netty的物联网高并发智能网关 JAVA版基于netty的物联网高并发智能网关 JAVA版基于netty的物联网高并发智能网关 JAVA版基于netty的物联网高并发智能网关 JAVA...
基于Netty的文件上传源码、基于socket通信的源码、基于udp协议通信的源码
他人用netty实现的一个聊天demo,包含server,client代码。代码较为简单。
netty-websocket-example 基于netty的websocket实现示例 高并发 附jmx压力测试
spring+netty+mybatis整合实例 实现一个简单的socket服务,启动服务后,客户端发送一条消息,服务端从数据库查询数据并返回给客户端。
基于netty框架编写的socket服务器