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();
} }

最新文章

  1. 玩转Unity资源,对象和序列化(下)
  2. JQuery常用方法总结
  3. gulp-babel 取消严格模式方法
  4. 访问cv::Mat中的数据时遇到的指针类型问题
  5. 关于struts2中表单提交时,中文乱码问题的解决
  6. buffer overflow
  7. linux C socket
  8. enter mysql
  9. PHP之SQL防注入代码集合(建站常用)
  10. LR_问题_如何将场景中的用户设置为百分比形式
  11. Android手机拨打电话的开发实例
  12. bzoj 3676: [Apio2014]回文串 回文自动机
  13. Linux Kernel ‘mp_get_count()’函数本地信息泄露漏洞
  14. 【译】在ASP.NET中创建PDF-iTextSharp起步
  15. bzoj1934
  16. Elasticsearch 分词器
  17. php sockent通信
  18. Android 安全提示 笔记
  19. ch3:文件处理与异常
  20. openshift 入门 部署 openshift-origin-server-v3.7.0

热门文章

  1. ffmpeg下载直播流
  2. 华为机试题 N阶楼梯的走法,每次走一步或者两步
  3. 文章预告的自我挖坑系列——D3.js 系列之星光闪烁
  4. 用CSS实现新闻轮播效果
  5. &lt;十七&gt;UML核心视图动态视图之时序图
  6. python反复执行某个命令
  7. bzoj 1004 Cards &amp; poj 2409 Let it Bead —— 置换群
  8. C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )
  9. C# 获取Console的输入和输出 数据 (异步)
  10. Ubuntu下Apache重启错误:Could not reliably determine解决