1、Netty软件包下载:http://netty.io/downloads.html,本示例基于netty-5.0.0.Alpha2.tar.bz2
2、搭建Netty应用工程
使用eclipse创建一个java工程,新建lib文件夹并将netty-all-5.0.0.Alpha2.jar复制到该文件夹下,然后将jar引入工程。
3、Netty服务端开发
TimeServer.java
package com.neety; 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{ //配置服务端Nio线程组 EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try{ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 1024) //.childHandler(new TimeServerHandler()); .childHandler(new ChildChannelHandler()); //绑定端口,同步等待成功 ChannelFuture f = b.bind(port).sync(); //等待服务端监听端口关闭 f.channel().closeFuture().sync(); }finally{ //退出时释放资源 bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{ @Override protected void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new TimeServerHandler()); } } public static void main(String[] args) throws Exception{ int port = 8083; if(args!=null && args.length > 0){ port = Integer.valueOf(args[0]); } new TimeServer().bind(port); } }
TimeServerHandler.java
package com.neety; import java.util.Date; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; public class TimeServerHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //super.channelRead(ctx, msg); ByteBuf buf = (ByteBuf) msg; 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 currentTime = "query time order".equalsIgnoreCase(body)?new Date( System.currentTimeMillis()).toString():"bad order"; ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes()); ctx.write(resp); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { //super.channelReadComplete(ctx); ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //super.exceptionCaught(ctx, cause); ctx.close(); } }
4、Netty客户端开发
TimeClient.java
package com.neety; 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{ //配置客户端NIO线程组 EventLoopGroup group = new NioEventLoopGroup(); try{ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); }; }); //发起异步连接操作 ChannelFuture f = b.connect(host,port).sync(); //等待客户端链路关闭 f.channel().closeFuture().sync(); }finally{ //退出,释放资源 group.shutdownGracefully(); } } public static void main(String[] args)throws Exception { int port = 8083; if(args!=null && args.length > 0){ port = Integer.valueOf(args[0]); } new TimeClient().connect(port, "127.0.0.1"); } }
TimeClientHandler.java
package com.neety; import java.util.logging.Logger; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerAdapter; import io.netty.channel.ChannelHandlerContext; /** * * @author Administrator * */ public class TimeClientHandler extends ChannelHandlerAdapter { private static final Logger logger = Logger.getLogger(TimeClientHandler.class.getName()); private final ByteBuf firstMessage; public TimeClientHandler() { byte [] req = "query time order".getBytes(); firstMessage = Unpooled.buffer(req.length); firstMessage.writeBytes(req); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { //super.channelActive(ctx); ctx.writeAndFlush(firstMessage); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { //super.channelRead(ctx, msg); ByteBuf buf = (ByteBuf)msg; byte [] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, "UTF-8"); System.out.println("now is :"+body); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //super.exceptionCaught(ctx, cause); logger.warning("unexpected exception from downstream:"+ cause.getMessage()); ctx.close(); } }
OVER
相关推荐
本工程采用maven+netty4.1.0+PrefixedStringDecoder+json技术,包括客户端和服务端。先运行服务端SampleServer,再去等客户端SampleClient。示例中发的是心跳包,其中消息格式定义为msgType + msgNo + content(json...
netty 入门Reactor示例
一个最简单的netty应用 使用:运行EchoServer,打开cmd命令窗口。输入telnet localhost 8080.成功连接后,输入字符,可在ide的控制台输出。 3.第二个示例 com.time’ Netty服务端与客户端,数据的发送与接收 ...
netty的知识,主要是解决拆包粘包的问题、内部含有客户端断线重连的代码,非常适合netty入门
本人很久没有上CSDN逛了,因为自从2012开始转入手机游戏开发行业之后,就没有时间上CSDN社区了。嘿嘿,还有好这么年过去了,偶居然还有...制作这个示例只是为给别人帮个忙而已,然后就可以带你入门,最后你自己飞^_^
博客文章对应的源代码,只是简单的使用,属于入门级别
Netty 4 的一些技术栈示例代码并辅以博文讲解。主要包括入门的demo,粘包和拆包解决办法,心跳测试,http服务的实现,client重连机制,TCP滑动窗口、protobuf协议传输等相关技术
用Netty5写一个简单的服务端和客户端,通过这个示例可以起到快速入门的效果,下载完成后,可运行4_Netty5_Hello模块代码,其中包括客户端单连接和多连接的例子
基于netty的聊天室Java示例基础代码,非常适用于入门级对概念的理解。可以运行,一线互联网公司大牛出品
该项目包含helloworld(快速入门)、web(ssh项目快速搭建)、aop(切面编程)、data-redis(redis缓存)、quartz(集群任务实现)、shiro(权限管理)、oauth2(四种认证模式)、shign(接口参数防篡改重放)、encoder(用户...
主要介绍了通过入门demo简单了解netty使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
的项目的示例模板。 Riposte 是一个基于 Netty 的微服务框架,用于快速开发生产就绪的 HTTP API。 它包括强大的功能,如分布式跟踪(由 Zipkin-compatible 提供)、错误处理和验证(默认提供的可插拔实现)和断路...
轻量级且快速:Helidon 旨在设计得简单易用,配有工具和示例,可帮助你快速上手。支持 Microprofile:Helidon 支持 MicroProfile 并提供熟悉的 API,如 JAX-RS, CDI 和 JSON-P/B。Helidon 的 MicroProfile 实现在 ...
由于历史原因,我将其留在 GitHub 上,并作为 JavaScript、JVM 和高性能 Java 网络库 (Netty) 之间重要集成的一个示例。ringo-evented:基于 Java Netty 项目的 RingoJS 事件化库ringo-evented的目标是使在 ...
6、支持多种通信框架(Mina/Netty/Grizzly),支持多种序列化/反序列化(Java/Hessian/PB); 7、支持自定义通信协议,可完全替换NFS-RPC自带的协议。 淘宝开放平台JAVA版SDK top4java 设计原则 容易维护扩展(不...
您可以通过单击其名称来下载样本,或克隆工作区,构建它,然后执行 mvn jetty:run 或者 mvn tomcat7:run 推荐的入门示例是聊天,它演示了使用AtmosphereHandler或jquery-pubsub进行的所有传输的用法,它们演示
概述 该项目是库的一个分支,该库从开始。 除了支持JUnit测试外,还对该库进行了大量的重构和代码修复,以确保该库适合生产使用。 该实现在主站和从站配置中均支持Modbus TCP,UDP,基于TCP的RTU,串行RTU和串行...
本项目示例基于spring boot最新版本(2.4.0)实现,Spring Boot,Spring Cloud学习示例,将持续更新…… 在基于Spring Boot,Spring Cloud的分布式微服务开发过程中,根据实际项目环境,需要选择,集成符合项目需求...
TCP客户端 使用SwiftNIO的TCP客户端示例。 入门 启动TCP服务器以侦听连接(例如 ) 克隆仓库 打开TCPClient.xcodeproj 选择要在My Mac上运行的TCPClient方案默认端口设置为3010 生成并运行
各种EJB之间的调用源码示例,用远程接口的引用访问EJB、函数将被FirstEJB调用,同时它将调用secondEJB 基于JAVA的UDP服务器模型源代码 2个目标文件 摘要:Java源码,网络相关,UDP 基于JAVA的UDP服务器模型源代码...