NettyServer
public class MyServer {
public static void main(String[] args)throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new MyInitializer());
ChannelFuture future = serverBootstrap.bind(8899).sync();
future.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
MyInitializer
public class MyInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("lengthFieldBasedFrameDecoder",
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("lengthFieldPrepender", new LengthFieldPrepender(4));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyHandler());
}
}
MyHandler
public class MyHandler extends SimpleChannelInboundHandler<String> {
private final static Logger logger = LoggerFactory.getLogger(MyHandler.class);
@Override
protected void messageReceived(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
logger.info("{}",channelHandlerContext.channel().remoteAddress() + ":" + msg);
channelHandlerContext.channel().writeAndFlush("from server: 草泥马");
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "********");
System.out.println("server handler added**********");
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "********");
System.out.println("server channel register****");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "********");
System.out.println("server channel actieve****");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
NettyClient
public class MyClient {
public static void main(String[] args) throws Exception{
EventLoopGroup bossGroup = new NioEventLoopGroup();
try{
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(bossGroup)
.channel(NioSocketChannel.class)
.handler(new MyClientInitializer());
ChannelFuture future = bootstrap.connect("127.0.0.1", 8899).sync();
future.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
}
}
}
MyClientInitializer
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("lengthFieldBasedFrameDecoder",
new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("lengthFieldPrepender", new LengthFieldPrepender(4));
pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyClientHandler());
}
}
MyClientHandler
public class MyClientHandler extends SimpleChannelInboundHandler<String> {
private final static Logger logger = LoggerFactory.getLogger(MyClientHandler.class);
@Override
protected void messageReceived(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
logger.info("{},{}",channelHandlerContext.channel().remoteAddress(),msg);
channelHandlerContext.channel().writeAndFlush("to Server: 草泥马");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "...........");
ctx.channel().writeAndFlush("来自于客户端的问候!");
System.out.println("client channel Active...");
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "...........");
System.out.println("client hanlder added...");
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(System.currentTimeMillis() + "...........");
System.out.println("client channel register...");
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channel inactive...");
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println("client channel unregister...");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
启动服务端和客户端发行之间可以相互通信