netty DelimiterBaseFrameDecoder 使用实例
来源:互联网 发布:mac怎么全屏 编辑:程序博客网 时间:2024/05/21 02:21
服务端代码:
package cc.zychen.com;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
public class EchoServer {
public void bind(int port) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
ByteBuf delimiter = Unpooled.copiedBuffer("@_".getBytes());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new EchoServerHandler());
}
});
//bind port
ChannelFuture f = b.bind(port).sync();
//wait
f.channel().closeFuture().sync();
}finally{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
/**
* @param args
*/
public static void main(String[] args) {
int port = 8080;
try {
if(args != null && args.length > 0){
port = Integer.parseInt(args[0]);
}
new EchoServer().bind(port);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端代码:
package cc.zychen.com;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.udt.nio.NioUdtProvider;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
public class EchoClient {
public void connect(String ip,int port) throws InterruptedException{
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap b = new Bootstrap();
b.group(group)
.option(ChannelOption.TCP_NODELAY, true)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ByteBuf delimiter = Unpooled.copiedBuffer("@_".getBytes());
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect(ip, port).sync();
f.channel().closeFuture().sync();
} finally{
group.shutdownGracefully();
}
}
/**
* @param args
*/
public static void main(String[] args) {
int port = 8080;
try {
if(args != null && args.length > 0){
port = Integer.parseInt(args[0]);
}
new EchoClient().connect("127.0.0.1", port);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
完整代码下载:http://download.csdn.net/download/byxdaz/10141627
- netty DelimiterBaseFrameDecoder 使用实例
- Netty使用实例
- Netty使用实例
- netty LengthFieldBasedFrameDecoder 使用实例
- netty的简单使用实例
- 实例:如何使用 Netty 下载文件
- 实例:如何使用 Netty 下载文件
- Netty 实例
- Netty实例
- Netty实例
- netty简单实例
- netty入门实例
- netty入门实例
- netty Demo 实例
- netty长连接实例
- netty入门实例
- netty入门实例
- netty简单实例
- 值类型变量和引用类型变量的区别
- xmlwriter写不进去文件,也不报错
- Thinkphp 实现多文件上传(包含html增加和删除控件)
- 2017ICPC沈阳站Tree题解
- Android activity堆栈及管理实例详解
- netty DelimiterBaseFrameDecoder 使用实例
- python的sqlalchemy模块
- TestMarkdown
- 百度地图相关整理
- Oracle把逗号分割的字符串转换为可放入in的条件语句的字符数列
- 电商大数据应用之用户画像
- 【机房】余额查询
- OpenStack-M版(Mitaka)搭建基于(Centos7.2)+++十一、Openstack块存储服务(cinder)
- 时间工具类