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.文档-- <书 ...
随机推荐
- Java基础知识强化60:经典查找之二分查找
1. 二分查找 二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好:其缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 比较 ...
- USB挂起与唤醒.
USB可见设备状态分为连接(Attached),上电(Powered),默认(Default),地址(Address),配置(Configured)和挂起(Suspended)6个状态.所谓可见,即U ...
- c - 对数组进行排序(通过指针的指针)
通过指针的指针,以及一个指针数组,对实际数组元素进行排序,有一个优点,就是排序过程交换的只有指针数组中的值,而不是实际的数组的元素.当实际元素中的对象很大,特别是结构体等类型时,这样做是很有好处. 下 ...
- oracle 查询前一小时、一天、一个月、一年的数据
查询一小时 select concat(to_char(sysdate,'yyyy-mm-dd ')||(to_char(sysdate,'hh24')-1),':00:00') start_time ...
- jQuery Alert Dialogs (Alert, Confirm, & Prompt代替方案)
基本范例--原文:http://keleyi.com/keleyi/phtml/jqplug/ Alert jAlert('自定义对话框', 'Alert对话框'); Confirm jConfirm ...
- Asp.Net--上传大文件(页面超时)
几个方法: 修改文件上传大小的限制 以文件形式保存到服务器 转换成二进制字节流保存到数据库 将二进制通过循环的方式写入磁盘 一.修改文件上传大小的限制 通过对web.config和machine.co ...
- webstrom的注释
今天我们小组的新同学有一个BUG调不好,然后我就帮他调一下.在调试的过程中非常累,纠其原因还是他注释写的不完善.我们可以看一下,他是这样写注释的(随便拿一个方法举例),如下图: 乍一看,是不是觉得他的 ...
- 根据反射生成SQL语句
/** * 基础查询语句 * 返回类型的属性字符串Sql * @author: InkYi * 修改时间:2016年5月11日 - 上午10:06:00<br/> * 功能说明:<b ...
- C程序设计语言练习题1-5
练习1-5 修改温度转换程序,要求以逆序(即按照从300度到0度的顺序)打印温度转换表. 代码如下: #include <stdio.h> // 包含标准库的信息. int main() ...
- S3C2440的GPIO
S3C2440一共有A B C D E F G H J 共九组IO口,一共是130个,每组IO口的个数如下图所示, 其中A组IO口只有输出功能,没有输入功能, 关于GPXCON寄存器,这个寄存器用来配 ...