书籍推荐:                                       实例代码 :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. Tree( 树) 组件[1]

    本节课重点了解 EasyUI 中 Tree(树)组件的使用方法, 这个组件依赖于 Draggable(拖动)和 Droppable(放置)组件. 一. 加载方式//class 加载方式<ul c ...

  2. 基于Spring MVC的简单HelloWorld实例

    1.导包 2.web.xml文件配置 3.包结构定义以及控制器的编写 4.xxxx-servlet文件配置 5.返回的视图(jsp)编写   6.源码 下载:http://download.csdn. ...

  3. VS2010字体优化

    文本编辑器:Consolas 环境字体:微软雅黑

  4. zip命令的使用

    zip命令可以用来将文件压缩成为常用的zip格式.unzip命令则用来解压缩zip文件. 1. 我想把一个文件abc.txt和一个目录dir1压缩成为yasuo.zip: # zip -r yasuo ...

  5. iOS9新特性之UIStackView

    同iOS以往每个迭代一样,iOS 9带来了很多新特性.UIKit框架每个版本都在改变,而在iOS 9比较特别的是UIStackView,它将从根本上改变开发者在iOS上创建用户界面的方式.本文将带你学 ...

  6. [c#]控制台进度条的示例

    看到[vb.net]控制台进度条的示例 感觉很好玩,翻译成C#版. using System; using System.Collections.Generic; using System.Linq; ...

  7. (一)HTML5 - pushState 无刷新更新地址

    可以解决什么问题: 可以实现网页的ajax加载,同时又能完成URL的改变而没有网页跳转刷新的迹象,就像是改变了网页的hash(#)一样. 优于hash: 避免了改变hash的问题,避免了用户不理解UR ...

  8. 矩阵链乘 hrbust 1600

    #include<string.h> //区间dp的思想#include<iostream> //将一个区间分成两段,将每一段当成是一个矩阵#include<stdio. ...

  9. mysql 5.7.9(GA) 安装

    mysql 5.7.9(GA) 终于发布了,感受一下. 一.下载 下载页面 http://dev.mysql.com/downloads/mysql/ 选择相应系统的版本下载. 本文OS为centos ...

  10. mysql 主从搭建

    主要搭建步骤如下: 1.打开binlog,设置server_id     打开主库的--log-bin,并设置server_id 2.主库授权                --最好也在从库对主库授权 ...