Netty(7-2)传List
2024-10-19 22:45:26
ObjectEchoServer
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new ObjectEncoder());
p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
p.addLast(new ObjectEchoServerHandler());
}
ObjectEchoServerHandler
public class ObjectEchoServerHandler extends ChannelInboundHandlerAdapter { @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.write(msg);
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
} }
ObjectEchoClient
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new ObjectEncoder());
p.addLast(new ObjectDecoder(ClassResolvers.cacheDisabled(null)));
p.addLast(new ObjectEchoClientHandler());
}
ObjectEchoClientHandler
public class ObjectEchoClientHandler extends ChannelInboundHandlerAdapter {
private final List<Integer> firstMessage; public ObjectEchoClientHandler() {//每个channel(connection)都会new该handler
firstMessage = new ArrayList<Integer>(ObjectEchoClient.SIZE);
for (int i = 0; i < ObjectEchoClient.SIZE; i++) {
firstMessage.add(Integer.valueOf(i));
}
} @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(firstMessage);
} @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//Echo back the received object to the server.
ctx.write(msg);
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
} }
最新文章
- 玩转Unity资源,对象和序列化(下)
- JQuery常用方法总结
- gulp-babel 取消严格模式方法
- 访问cv::Mat中的数据时遇到的指针类型问题
- 关于struts2中表单提交时,中文乱码问题的解决
- buffer overflow
- linux C socket
- enter mysql
- PHP之SQL防注入代码集合(建站常用)
- LR_问题_如何将场景中的用户设置为百分比形式
- Android手机拨打电话的开发实例
- bzoj 3676: [Apio2014]回文串 回文自动机
- Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞
- 【译】在ASP.NET中创建PDF-iTextSharp起步
- bzoj1934
- Elasticsearch 分词器
- php sockent通信
- Android 安全提示 笔记
- ch3:文件处理与异常
- openshift 入门 部署 openshift-origin-server-v3.7.0
热门文章
- ffmpeg下载直播流
- 华为机试题 N阶楼梯的走法,每次走一步或者两步
- 文章预告的自我挖坑系列——D3.js 系列之星光闪烁
- 用CSS实现新闻轮播效果
- <;十七>;UML核心视图动态视图之时序图
- python反复执行某个命令
- bzoj 1004 Cards &; poj 2409 Let it Bead —— 置换群
- C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
- C# 获取Console的输入和输出 数据 (异步)
- Ubuntu下Apache重启错误:Could not reliably determine解决