Board logo

标题: Netty 超时机制及心跳程序实现(2) [打印本页]

作者: look_w    时间: 2018-12-24 20:45     标题: Netty 超时机制及心跳程序实现(2)

服务器服务器代码比较简单,启动后侦听 8082 端口
public final class HeartbeatServer {    static final int PORT = 8082;    public static void main(String[] args) throws Exception {        // Configure the server.        EventLoopGroup bossGroup = new NioEventLoopGroup(1);        EventLoopGroup workerGroup = new NioEventLoopGroup();        try {            ServerBootstrap b = new ServerBootstrap();            b.group(bossGroup, workerGroup)             .channel(NioServerSocketChannel.class)             .option(ChannelOption.SO_BACKLOG, 100)             .handler(new LoggingHandler(LogLevel.INFO))             .childHandler(new HeartbeatHandlerInitializer());            // Start the server.            ChannelFuture f = b.bind(PORT).sync();            // Wait until the server socket is closed.            f.channel().closeFuture().sync();        } finally {            // Shut down all event loops to terminate all threads.            bossGroup.shutdownGracefully();            workerGroup.shutdownGracefully();        }    }}
客户端测试客户端用操作系统自带的 Telnet 程序即可:
telnet 127.0.0.1 8082
效果




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0