基于Netty实现的简易服务端与客户端的信息交流
程序员文章站
2022-06-06 14:34:03
...
1:服务器端所需要的三个类如下:
public class TimeServer {
public static void main(String[] agrs) {
new TimeServer().bind(8080);
}
private void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap sb = new ServerBootstrap();
sb.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 1024)
.childHandler(new ChildChannelHandler());
ChannelFuture f = sb.bind(port).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
e.printStackTrace();
}
}
}
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new TimeServerHandler());
}
}
public class TimeServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf bfBuf = (ByteBuf) msg;
byte[] bytes = new byte[bfBuf.readableBytes()];
bfBuf.readBytes(bytes);
String message = new String(bytes,"UTF-8");
System.out.println("the requestMessage is:"+message);
ByteBuf responseBuffer = Unpooled.copiedBuffer("the response message from server".getBytes());
ctx.write(responseBuffer);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
ctx.close();
}
}
2:客户端实现了两个类
public class TimeClient {
public static void main(String[] args) {
new TimeClient().connect("127.0.0.1",8080);
}
private void connect(String ip, int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bs = new Bootstrap();
bs.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new TimeClientHandler());
}
});
ChannelFuture cf = bs.connect(ip,port).sync();
cf.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
public class TimeClientHandler extends ChannelInboundHandlerAdapter {
private final ByteBuf buffer;
public TimeClientHandler () {
String message = "quest message";
buffer = Unpooled.buffer(message.length());
buffer.writeBytes(message.getBytes());
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(buffer);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf responseMessage = (ByteBuf) msg;
byte[] bytes = new byte[responseMessage.readableBytes()];
responseMessage.readBytes(bytes);
String resMsg = new String(bytes, "UTF-8");
System.out.println(resMsg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}