书籍推荐:                                       实例代码 :http://download.csdn.net/detail/jiangtao_st/7677503


  1. Netty Server端实现

    /**
    *
    * <p>
    * Netty Server Simple
    * </p>
    *
    * @author 卓轩
    * @创建时间:2014年7月7日
    * @version: V1.0
    */ public class NettyServer { private final int port = 8989; @Test
    public void nettyServer(){ EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup,workerGroup)
    .channel(NioServerSocketChannel.class)
    .option(ChannelOption.SO_BACKLOG, 1024)
    .childHandler(new ChildChannelHandler()); //绑定端口、同步等待
    ChannelFuture futrue = serverBootstrap.bind(port).sync(); //等待服务监听端口关闭
    futrue.channel().closeFuture().sync();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //退出,释放线程等相关资源
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
    } } private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
    @Override
    protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new SimpleServerHandler());
    }
    } }
  2. Netty Client 实现

    /**
    *
    * <p>
    * NettyClient 实现
    * </p>
    *
    * @author 卓轩
    * @创建时间:2014年7月7日
    * @version: V1.0
    */
    public class NettyClient { public void connect(int port,String host){ EventLoopGroup group = new NioEventLoopGroup(); try {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group)
    .channel(NioSocketChannel.class)
    .option(ChannelOption.TCP_NODELAY, true)
    .handler(new ChannelInitializer<SocketChannel>() { @Override
    protected void initChannel(SocketChannel ch) throws Exception {
    ch.pipeline().addLast(new SimpleClientHandler());
    }
    });
    //发起异步链接操作
    ChannelFuture channelFuture = bootstrap.connect(host, port).sync(); channelFuture.channel().closeFuture().sync();
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    //关闭,释放线程资源
    group.shutdownGracefully();
    }
    } @Test
    public void nettyClient(){ new NettyClient().connect(8989, "localhost");
    } }
  3. ServerHander 处理程序 

    /**
    *
    * <p>
    * Server接收消息处理Handler
    * </p>
    *
    * @author 卓轩
    * @创建时间:2014年7月7日
    * @version: V1.0
    */
    public class SimpleServerHandler extends ChannelInboundHandlerAdapter { @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf)msg;
    byte [] req = new byte[buf.readableBytes()]; buf.readBytes(req); String message = new String(req,"UTF-8"); System.out.println("Netty-Server:Receive Message,"+ message); }
    }
  4. ClientHander 处理程序

    /**
    *
    * <p>
    * Client Handler
    * </p>
    *
    * @author 卓轩
    * @创建时间:2014年7月7日
    * @version: V1.0
    */
    public class SimpleClientHandler extends ChannelInboundHandlerAdapter { private ByteBuf clientMessage; public SimpleClientHandler() { byte [] req = "Call-User-Service".getBytes();
    clientMessage = Unpooled.buffer(req.length);
    clientMessage.writeBytes(req);
    } @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.writeAndFlush(clientMessage); } @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf)msg;
    byte [] req = new byte[buf.readableBytes()]; buf.readBytes(req); String message = new String(req,"UTF-8"); System.out.println("Netty-Client:Receive Message,"+ message);
    } @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { ctx.close();
    }
    }

Netty实例-简单的服务端-client实现,凝视具体的更多相关文章

  1. 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)

    Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...

  2. Netty入门——客户端与服务端通信

    Netty简介Netty是一个基于JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性.换句话说,Netty是一个NIO框架,使用它可以简单快速 ...

  3. Netty入门一:服务端应用搭建 & 启动过程源码分析

    最近周末也没啥事就学学Netty,同时打算写一些博客记录一下(写的过程理解更加深刻了) 本文主要从三个方法来呈现:Netty核心组件简介.Netty服务端创建.Netty启动过程源码分析 如果你对Ne ...

  4. WCF 项目应用连载[3] - 双向通信 实例管理与服务端监控

    WCF 项目应用连载[1] - 索引 - 轻量级的Log系统 - Lig Sample -序 第二节我们已经创建了Lig项目,并且能稳定工作了.现在我们来改进ILigAgent接口,实现WCF的双向通 ...

  5. socket编程,简单多线程服务端测试程序

    socket编程,简单多线程服务端测试程序 前些天重温了MSDN关于socket编程的WSAStartup.WSACleanup.socket.closesocket.bind.listen.acce ...

  6. winsock 编程(简单客户&服务端通信实现)

    winsock 编程(简单客户&服务端通信实现) 双向通信:Client send message to Server, and if  Server receive the message, ...

  7. 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)

    后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...

  8. Netty源码解析---服务端启动

    Netty源码解析---服务端启动 一个简单的服务端代码: public class SimpleServer { public static void main(String[] args) { N ...

  9. 书剑恩仇录online全套源代码(服务端+client+文档)

    书剑恩仇录online全套源代码(服务端+client+文档).vc++开发,解压后将近10G大小,眼下网上最完整版本号,包括client源代码.服务端源代码.工具源代码.sdk.文档-- <书 ...

随机推荐

  1. [Polymer] Introduction

    install Polymer and explore creating our first custom element: bower install polymer index.html: < ...

  2. mybatis与spring的整合

    今天是mybatis的最后一天,也是最为重要的一天,mybatis与spring整合,(spring相关知识我会抽一个大的模块进行讲解). 首先加入Spring的依赖 <dependency&g ...

  3. 使用Razor来进行页面布局

    UI设计师们现在也讲究页面设计的语义化和结构化,把一个页面分成很多个模块,使用语义化的类名或id来标识这些模块.Razor推出了新的布局解决方案来迎合这一潮流. 这里涉及到Razor的一些语法,大家可 ...

  4. 对于数据操作的SQL语句精粹(长期更新)

    --删除空格 Update [Table] Set [Column]=Replace([Column],' ','') --查出左右和右边带空格的数据 select RTRIM( LTRIM([Col ...

  5. 触发器内insert,delete,update判断执行不同的内容

    create trigger tr_aon afor insert,update,delere asbegin IF EXISTS(SELECT 1 FROM inserted) AND NOT EX ...

  6. java 线程池的用法

    1.java自带的类ExecutorService用于提供线程池服务,可以一下方法初始化线程池: ExecutorService pool = Executors.newFixedThreadPool ...

  7. java下tcp的socket连接

    serverDemo package cn.stat.p4.ipdemo; import java.io.IOException; import java.io.InputStream; import ...

  8. (原)torch中微调某层参数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6221664.html 参考网址: https://github.com/torch/nn/issues ...

  9. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  10. 解决SurfaceView设置透明造成覆盖其他组件的替代方案

    之前在项目里面绘制摇杆圆盘使用SurfaceView来实现,同时设置SurfaceView透明,但是这样会造成SurfaceView的组件会覆盖其他的组件,一般情况没有关系,而不一般的情况就是有类似上 ...