use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.
the class NettyClient method main.
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
*
* @param args the input arguments
* @throws Exception the exception
*/
public static void main(String[] args) throws Exception {
try {
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
p.addLast(new BaseClient1Handler());
p.addLast(new BaseClient2Handler());
}
});
// 连接服务端
ChannelFuture future = b.connect(host, port).sync();
System.out.println("客户端连接成功!");
// 发送消息
future.channel().writeAndFlush("Hello Netty Server ,I am a common client");
// 关闭
future.channel().closeFuture().sync();
} finally {
// 释放资源
group.shutdownGracefully();
}
// start();
}
use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.
the class NettyServerDemo5 method start.
/**
* Start.
*/
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// 入参说明: 读超时时间、写超时时间、所有类型的超时时间、时间格式
p.addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
// String解码器
p.addLast(new StringDecoder());
// String编码器
p.addLast(new StringEncoder());
// 绑定自定义业务逻辑
p.addLast(new NettyServerHandlerDemo5());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收进来的连接
ChannelFuture future = sbs.bind(port).sync();
System.out.println("Netty服务端启动成功,端口为: " + port);
// 释放监听
future.channel().closeFuture().sync();
} catch (Exception e) {
// 释放资源
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.
the class NettyServer method start.
/**
* Netty创建全部都是实现自AbstractBootstrap。
* 客户端的是Bootstrap,服务端的则是 ServerBootstrap。
*
* @throws InterruptedException the interrupted exception
*/
public static void start() throws InterruptedException {
// 引导辅助程序
ServerBootstrap sb = new ServerBootstrap();
// 通过nio方式来接收连接和处理连接
EventLoopGroup group = new NioEventLoopGroup();
try {
// 通过nio方式来接收连接和处理连接
sb.group(group);
// 设置nio类型的channel
sb.channel(NioServerSocketChannel.class);
sb.childHandler(new // 有连接到达时会创建一个channel
ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// 字符串解码 和 编码
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
// 在channel队列中添加一个handler来处理业务
p.addLast("handler", new NettyServerHandler());
// 以("\n")为结尾分割的 解码器
// cp.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
}
});
// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
ChannelFuture f = sb.bind(port).sync();
System.out.println("服务端已启动... 端口是:" + port);
// 应用程序会一直等待,直到channel关闭
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
// group.shutdownGracefully().sync();//关闭EventLoopGroup,释放掉所有资源包括创建的线程
}
}
use of io.netty.handler.codec.string.StringDecoder in project pancm_project by xuwujing.
the class NettyServerDemo2 method start.
/**
* Start.
*/
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap sbs = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
// 字节解码器 ,其中2048是规定一行数据最大的字节数。 用于解决拆包问题
p.addLast(new LineBasedFrameDecoder(2048));
// p.addLast(new FixedLengthFrameDecoder(100)); //定长数据帧的解码器 ,每帧数据100个字节就切分一次。 用于解决粘包问题
// p.addLast(new DelimiterBasedFrameDecoder(1024,Unpooled.copiedBuffer("~_~".getBytes()))); //固定字符切分解码器 ,会以"~_~"为分隔符。 注意此方法要放到StringDecoder()上面
// 设置解码器
p.addLast(new StringDecoder());
// 绑定自定义事物
p.addLast(new NettyServerHandlerDemo2());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
// 绑定端口,开始接收进来的连接
ChannelFuture future = sbs.bind(port).sync();
System.out.println("服务端启动成功,端口为 :" + port);
future.channel().closeFuture().sync();
} catch (Exception e) {
// 关闭EventLoopGroup,释放掉所有资源包括创建的线程
bossGroup.shutdownGracefully();
// 关闭EventLoopGroup,释放掉所有资源包括创建的线程
workerGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.string.StringDecoder in project sidewinder by srotya.
the class GraphiteServer method start.
@Override
public void start() throws Exception {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(2);
ServerBootstrap bs = new ServerBootstrap();
channel = bs.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_RCVBUF, 10485760).handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(workerGroup, new LineBasedFrameDecoder(1024, true, true));
p.addLast(workerGroup, new StringDecoder());
p.addLast(workerGroup, new GraphiteDecoder(dbName, storageEngine, writeCounter));
}
}).bind(bindAddress, serverPort).sync().channel();
}
Aggregations