Netty学习
2016-04-26 09:35
309 查看
1.Dependency:
2.单服务器,多客户端例子:
2.1.创建一个消息处理的Handler,它继承ChannelInboundHandlerAdapter
package leo.test;
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;
/**
* @author Leo
* @date 2016年4月25日下午2:43:49
* @description
* @usage
*/
public class Server {
private int port;
public Server(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new MsgHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to
// gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
2.3.测试:
2.3.1.先启动server
2.3.2.1.进入cmd
2.3.2.2.输入 telnet localhost 9527
2.3.2.3.随便输入点东西,就可以看到客户端与服务器的交互了.
参考资料:http://netty.io/wiki/user-guide-for-4.x.html
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.0.CR7</version> </dependency>
2.单服务器,多客户端例子:
2.1.创建一个消息处理的Handler,它继承ChannelInboundHandlerAdapter
package leo.test; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.util.CharsetUtil; /** * @author Leo * @date 2016年4月25日下午2:41:52 * @description * @usage */ public class MsgHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2) ByteBuf in = (ByteBuf) msg;//接收 ByteBuf out = ctx.alloc().buffer();//发送 System.out.println("Rcv:" + in.toString(CharsetUtil.UTF_8));//打印接收到的消息 out.writeBytes("Server have rcv the msg.".getBytes(CharsetUtil.UTF_8));//将要发送的消息以byte形式存在out中 ctx.writeAndFlush(out);//通过ctx输出到接收端. } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4) // Close the connection when an exception is raised. cause.printStackTrace(); ctx.close(); } }2.2.根据官网的demo创建一个Server
package leo.test;
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;
/**
* @author Leo
* @date 2016年4月25日下午2:43:49
* @description
* @usage
*/
public class Server {
private int port;
public Server(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new MsgHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to
// gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
2.3.测试:
2.3.1.先启动server
package leo.test; import org.junit.Test; /** * @author Leo * @date 2016年4月26日上午9:29:16 * @description * @usage */ public class MyTest { @Test public void test01() { try { new Server(9527).run();//port number:9527 } catch (Exception e) { e.printStackTrace(); } } }2.3.2.如果是在window下,你可以很方便的用telnet进行测试.
2.3.2.1.进入cmd
2.3.2.2.输入 telnet localhost 9527
2.3.2.3.随便输入点东西,就可以看到客户端与服务器的交互了.
参考资料:http://netty.io/wiki/user-guide-for-4.x.html
相关文章推荐
- 比萨斜塔——统计显著性检验
- java里面的枚举enum
- SURVIVAL SHOOTER TUTORIAL之Environment setup [1]
- CANopen系列文章【1】--SYNC【3】
- 自学
- 第11课 Qt中的字符串类
- win commands
- 工具UIAppearance
- CSS学习笔记(三)背景
- 常用的正则表达式
- 最难面试问题?这样回答才满意!
- 数据安全与保密(摘抄)
- SQL 时间戳转DateTime类型
- 钉钉客户端JS-API权限签名算法.NET版
- android 从sdcard安装apk,点击home键,出现问题
- UIAlertController
- JS调用Activity方法时,方法里的overridePendingTransition 不起作用
- vector用sort算法排序
- 学习Unity官方教程-Survival Shooter tutorialの声明
- book