Netty
NettyServer
package netty01;
import java.net.InetSocketAddress;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class NettyServer {
private static final int PORT = 6666;
public static void main(String[] args) throws Exception {
new NettyServer().start();
}
public void start() {
EventLoopGroup parentGroup = new NioEventLoopGroup();
EventLoopGroup childGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(parentGroup, childGroup)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(PORT))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind().sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
}
}
}
ServerHandler
package netty01;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class ServerHandler extends ChannelInboundHandlerAdapter{
private Logger logger = LoggerFactory.getLogger(ServerHandler.class);
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.debug("exceptionCaught: "+cause.getMessage());
cause.printStackTrace();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.debug("----------server--------channelRead ");
logger.debug("msg所属类型: "+msg.getClass().getName());
ByteBuf in = (ByteBuf)msg;
logger.debug("channelRead: "+in.toString(CharsetUtil.UTF_8));
ctx.writeAndFlush("hello client");
ctx.write("client....");
ctx.flush();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
logger.debug("----------server--------channelReadComplete ");
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.debug("---------server---------channelActive ");
}
}
NettyClient
package netty01;
import java.net.InetSocketAddress;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class NettyClient {
private static final String IP = "localhost";
private static final int PORT = 6666;
public static void main(String[] args) {
new NettyClient().start();
}
public void start() {
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(IP, PORT))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientHandle());
}
});
ChannelFuture future = bootstrap.connect().sync();
future.channel().closeFuture().sync();
}catch (InterruptedException e) {
e.printStackTrace();
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
ClientHandle
package netty01;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
public class ClientHandle extends SimpleChannelInboundHandler<ByteBuf>{
private Logger logger = LoggerFactory.getLogger(ClientHandle.class);
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
logger.debug("---------client---------channelActive ");
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rock", CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
logger.debug(cause.getMessage());
cause.printStackTrace();
ctx.close();
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
logger.debug("---------clients---------channelRead0 ");
logger.debug("client msg:"+msg.toString(CharsetUtil.UTF_8));
}
}