Netty实例-简单的服务端-client实现,凝视具体
- 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());
}
} } - 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");
} } - 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); }
} - 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实现,凝视具体的更多相关文章
- 实例PK(Vue服务端渲染 VS Vue浏览器端渲染)
Vue 2.0 开始支持服务端渲染的功能,所以本文章也是基于vue 2.0以上版本.网上对于服务端渲染的资料还是比较少,最经典的莫过于Vue作者尤雨溪大神的 vue-hacker-news.本人在公司 ...
- Netty入门——客户端与服务端通信
Netty简介Netty是一个基于JAVA NIO 类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性.换句话说,Netty是一个NIO框架,使用它可以简单快速 ...
- Netty入门一:服务端应用搭建 & 启动过程源码分析
最近周末也没啥事就学学Netty,同时打算写一些博客记录一下(写的过程理解更加深刻了) 本文主要从三个方法来呈现:Netty核心组件简介.Netty服务端创建.Netty启动过程源码分析 如果你对Ne ...
- WCF 项目应用连载[3] - 双向通信 实例管理与服务端监控
WCF 项目应用连载[1] - 索引 - 轻量级的Log系统 - Lig Sample -序 第二节我们已经创建了Lig项目,并且能稳定工作了.现在我们来改进ILigAgent接口,实现WCF的双向通 ...
- socket编程,简单多线程服务端测试程序
socket编程,简单多线程服务端测试程序 前些天重温了MSDN关于socket编程的WSAStartup.WSACleanup.socket.closesocket.bind.listen.acce ...
- winsock 编程(简单客户&服务端通信实现)
winsock 编程(简单客户&服务端通信实现) 双向通信:Client send message to Server, and if Server receive the message, ...
- 客户端(springmvc)调用netty构建的nio服务端,获得响应后返回页面(同步响应)
后面考虑通过netty做一个真正意义的简约版RPC框架,今天先尝试通过正常调用逻辑调用netty构建的nio服务端并同步获得返回信息.为后面做铺垫 服务端实现 我们先完成服务端的逻辑,逻辑很简单,把客 ...
- Netty源码解析---服务端启动
Netty源码解析---服务端启动 一个简单的服务端代码: public class SimpleServer { public static void main(String[] args) { N ...
- 书剑恩仇录online全套源代码(服务端+client+文档)
书剑恩仇录online全套源代码(服务端+client+文档).vc++开发,解压后将近10G大小,眼下网上最完整版本号,包括client源代码.服务端源代码.工具源代码.sdk.文档-- <书 ...
随机推荐
- Spring + mybatis整合方案总结 结合实例应用
Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...
- Linux能力(capability)机制的继承
1.Linux能力机制概述 在以往的UNIX系统上,为了做进程的权限检查,把进程分为两类:特权进程(有效用户ID是0)和非特权进程(有效用户ID是非0).特权进程可以通过内核所有的权限检查,而非特权进 ...
- 使用VS Code开发AngularJS 2 第一个应用程序
使用VS Code开发AngularJS 2 第一个应用程序 目录 运行环境 创建项目 安装依赖包 创建TypeScript应用程序 配置应用程序 运行应用程序 运行环境 运行环境: Windows ...
- nginx log日志分割
@echo offrem 备份并根据时间重命名错误日志文件set "cmdstr=move E:\nginx\logs\error.log E:\nginx\logs\error%date: ...
- Swift - IBOutlet返回nil(fatal error: unexpectedly found nil while unwrapping an Optional value)
在Swift 中 ViewController 默认构造方法不关联同名的xib文件 在使用OC的时候,调用ViewController的默认构造函数,会自动关联到一个与ViewController名字 ...
- Slony-I双机备份
测试环境:postgresql 9.3.5,slony-I2.2.3(application stack builder提供)以下参考网上教程亲自测试总结 ---------------------- ...
- 带权并查集 poj1182
首先要注意核心代码 int find(int i){ if(i == fa[i]) return fa[i]; int tt = find(fa[i]); num[i] ...
- 手机网页中的hover效果实现
js文件 function mouseout(obj) { var className ="hover"; var _ecname = obj.className; if (_ec ...
- Jquery简略API使用
都是个人随手笔记,既然开通了博客园就分享给大家.谨做为参考,具体大家自己测试以及使用 ★ $() ★ JQ的一个万能获取对象的函数(获取跟CSS获取元素是一样的)$(function(){}); 替代 ...
- 利用redis协助mysql数据库搬迁
最近公司新项目上线,需要数据库搬迁,但新版本和老版本数据库差距比较大,关系也比较复杂.如果用传统办法,需要撰写很多mysql脚本,工程量虽然不大,但对于没有dba的公司来说,稍微有点难度.本人就勉为其 ...