netty hello world
来源:互联网 发布:c语言汉诺塔递归 编辑:程序博客网 时间:2024/05/29 11:39
入门文档
public class ServerHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)throws Exception {
// ((ByteBuf) msg).release();
ByteBuf buf = (ByteBuf)msg;
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
String request = new String(data,"utf-8");
System.out.println("server:"+request);
//写给客户端
String response = "我是server反馈信息";
ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes()));
// .addListener(ChannelFutureListener.CLOSE); 写完之后 客户端收到消息 就会关掉连接
// ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
cause.printStackTrace();
ctx.close();
}
}
public class Server {
public static void main(String[] args) throws Exception {
//第一个线程组 接受client端
EventLoopGroup bossGroup = new NioEventLoopGroup();
//第二个线程组 实际业务处理
EventLoopGroup workerGroup = new NioEventLoopGroup();
//3 创建一个辅助类Bootstrap,就是对我们的Server进行一系列的配置
ServerBootstrap b = new ServerBootstrap();
//把两个工作线程组加进来
b.group(bossGroup, workerGroup)
//指定使用NioServerSocketChannel
.channel(NioServerSocketChannel.class)
//一定要使用 childHandler 去绑定具体的 事件处理器
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ServerHandler());//这里可以加多个处理器
}
});
//设置tcp的缓冲区
// .option(ChannelOption.SO_BACKLOG, 128)
//保持连接
// .option(ChannelOption.SO_KEEPALIVE, true);
//绑定指定端口 进行监听
ChannelFuture f = b.bind(8920).sync();
Thread.sleep(1000000);
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public class ClientHandler extends ChannelHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
try{
ByteBuf buf = (ByteBuf)msg;
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
String request = new String(data,"utf-8");
System.out.println("client:"+request);
} finally {
//没有调用write方法 需要手动释放
ReferenceCountUtil.release(msg);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}
public class Client {
public static void main(String[] args) throws Exception {
EventLoopGroup workGroup = new NioEventLoopGroup();
Bootstrap b = new Bootstrap();
b.group(workGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel sc) throws Exception {
sc.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture f = b.connect("127.0.0.1",8920).sync();
//buf
f.channel().writeAndFlush(Unpooled.copiedBuffer("777".getBytes()));
// f.channel().flush();
f.channel().closeFuture().sync();
workGroup.shutdownGracefully();
}
}
- netty hello world
- Netty初步之hello world
- Netty初步之hello world
- Netty初步之Hello World
- Netty初步之hello world
- Netty初步之Hello World
- Netty初步之Hello World
- netty 入门案例hello world
- Netty构建游戏服务器(二)--Hello World
- 《Netty学习》(二)Hello World
- Java NIO框架Netty教程(十七) Netty4 Hello world
- netty学习(二)——Hello world!
- Netty 中文教程 (二) Hello World !详解
- Netty 中文教程 (二) Hello World !详解
- Netty 中文教程 (二) Hello World !详解
- Marshalling在netty中的基本使用(Hello World)
- JAVA Socket编程学习9--Netty入门之Hello World!
- Hello World!【Hello World】
- 并发模型(二)——Master-Worker模式
- pygal输出png问题:dlopen() failed to load a library: cairo / cairo-2
- Android Recyclerview判断是否已经到底部或者顶部
- Android 客户端 MVP+Dagger2+Databinding+Rxjava+Retrofit+Okhttp3
- ARKit基本使用流程
- netty hello world
- SQL循环语句的几种方式
- HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结
- curses
- java 位运算
- xml之DOM解析
- 修改终端的hostName
- os_cpu.h
- [UCI] OpenWrt-uci命令系统