netty入门demo
参考博客:(14条消息) 【Netty整理01-快速入门】Netty简单使用Demo(已验证)_the_fool_的博客-CSDN博客
ServerHandler.java
package com.hmb;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("server read....");
ByteBuf buf = (ByteBuf) msg;
byte[] date = new byte[buf.readableBytes()];
buf.readBytes(date);
System.out.println("client msg: " + new String(date));
buf.release();
System.out.println("send msg to client...");
String response = "hello client";
ByteBuf buf1 = ctx.alloc().buffer();
buf1.writeBytes(response.getBytes());
ctx.write(buf1);
ctx.flush();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
}
NettyServer.java
package com.hmb;
import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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 int port = 9999;
public void start() {
EventLoopGroup worker = new NioEventLoopGroup();
EventLoopGroup boss = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, worker)
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ServerHandler());
}
});
try {
ChannelFuture cf = serverBootstrap.bind(port).sync();
cf.channel().closeFuture() .sync();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
public static void main(String[] args) {
NettyServer server = new NettyServer();
server.start();
}
}
ClientHandler.java
package com.hmb;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("read msg from server...");
ByteBuf buf = (ByteBuf) msg;
byte[] date = new byte[buf.readableBytes()];
buf.readBytes(date);
System.out.println("server msg: " + new String(date));
buf.release();
Thread.sleep(10000);
System.out.println("send msg to server...");
String response = "hello server";
ByteBuf buf1 = ctx.alloc().buffer();
buf1.writeBytes(response.getBytes());
ctx.write(buf1);
ctx.flush();
}
/**
* channel生命周期回调函数,channel准备好后回调此函数,不能少,否则bs交互无法发起
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("firstly send msg to server...");
String msg = "hello server!";
ByteBuf encode = ctx.alloc().buffer(4 * msg.length());
encode.writeBytes(msg.getBytes());
ctx.write(encode);
ctx.flush();
}
}
NettyClient.java
package com.hmb;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class NettyClient {
public void connect(String host, int port) {
EventLoopGroup worker = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(worker)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ClientHandler());
}
});
try {
ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
worker.shutdownGracefully();
}
}
public static void main(String[] args) {
NettyClient nettyClient = new NettyClient();
nettyClient.connect("127.0.0.1", 9999);
}
}
先运行NettyServer.java的main方法,再运行NettyClient.java的main方法,此时就可以看到bs的交互信息



浙公网安备 33010602011771号