w ServerBootstrap();
sb.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
// .childHandler(new ChildChannelHandler())
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
channel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
channel.pipeline().addLast(new ServerHandler());
}
})
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture cf = sb.bind(port).sync();
System.out.println("服务端已启动");
cf.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static class ChildChannelHandler extends ChannelInitializer {
protected void initChannel(Channel channel) throws Exception {
channel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
channel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());
channel.pipeline().addLast(new ServerHandler());
}
}
public static void main(String[] args){
try {
new MsgServer().bind(8080);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.netty.serialize.handler;
import com.netty.serialize.message.Message;
import com.netty.serialize.message.MsgHeader;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
/**
* 用于测试服务端实现的
* Created by sdc on 2017/8/29.
*/
public class ServerHandler extends ChannelHandlerAdapter{
&