版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_Jsp_Ssh/article/details/82709957
NettyServer 端 服务端
public class NettyServer {
public static void main(String[] args) throws Exception{
NettyServer.start();
}
public static void start() throws Exception{
final NettyServerHandler serverHandler = new NettyServerHandler();
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup,workGroup).channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG,1024)//设置TCP缓冲区
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
// TODO Auto-generated method stub
ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture f = b.bind(8000).sync();
f.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully().sync();
workGroup.shutdownGracefully().sync();
}
}
}
建立连接时,会调用channelHandler接口实现类,这里就是个人自定义实现逻辑的地方;其他代码都是模板方法
serverHandler
public class NettyServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// TODO Auto-generated method stub
ByteBuf in = (ByteBuf)msg;
byte[] req = new byte[in.readableBytes()];
in.readableBytes();
System.out.println("sever received:" + in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
// TODO Auto-generated method stub
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// TODO Auto-generated method stub
cause.printStackTrace();
ctx.close();
}
}
channelHandler接口方法比较多,其实我们主要用的就是chanelRead,exceptionCaught方法等。其他的可以交给别的实现类去执行。
nettyClient端
public class NettyClientServer {
public static void main(String[] args) throws Exception{
NettyClientServer.start();
}
public static void start()throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress("127.0.0.1",8000))
.handler(new ChannelInitializer<Channel>() {
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new NettyClientServerHandler());
};
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}
clientHandler,客户端连接成功后,会触发channelHandler接口实现类中的激活方法channelActive,此时与服务器之间的连接建立成功;
public class NettyClientServerHandler extends SimpleChannelInboundHandler<ByteBuf>{
@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
// TODO Auto-generated method stub
System.out.println("client received:" + msg.toString(CharsetUtil.UTF_8));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf message = null;
for(int i= 0 ; i<100;i++) {
message = Unpooled.buffer("Netty rocks!".getBytes().length);
message.writeBytes("Netty rocks!".getBytes());
ctx.writeAndFlush(message);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}