使用Netty框架实现一个简单的C/S结构的网络程序 处理简单的业务逻辑 在server端至少编写2个ChannelHandler
时间: 2024-04-29 11:25:27 浏览: 92
基于netty实现的web框架
下面是一个简单的C/S结构的网络程序示例,使用Netty框架实现。该程序的业务逻辑为,客户端向服务端发送一个字符串,服务端将该字符串转换为大写,并返回给客户端。
Server端:
```java
public class Server {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new UpperCaseServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(8888).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}
```
在Server端的ChannelHandler中,我们至少编写了2个Handler,分别为StringDecoder和UpperCaseServerHandler。
StringDecoder用于将ByteBuf类型的数据解码成String类型的数据。
UpperCaseServerHandler用于将接收到的字符串转换为大写,并返回给客户端。
```java
public class UpperCaseServerHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
String result = msg.toUpperCase();
ctx.write(result);
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
```
Client端:
```java
public class Client {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new UpperCaseClientHandler());
}
});
ChannelFuture f = b.connect("localhost", 8888).sync();
f.channel().writeAndFlush("hello, world!").sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
```
在Client端的ChannelHandler中,我们只编写了一个Handler,即UpperCaseClientHandler。
UpperCaseClientHandler用于接收服务端返回的字符串,并打印出来。
```java
public class UpperCaseClientHandler extends SimpleChannelInboundHandler<String> {
@Override
public void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println("Server response: " + msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
```
上述程序中,我们使用了StringDecoder和StringEncoder两个Handler,它们分别用于将ByteBuf类型的数据解码成String类型的数据,以及将String类型的数据编码成ByteBuf类型的数据。此外,我们还使用了UpperCaseServerHandler和UpperCaseClientHandler两个Handler,它们分别用于处理服务端和客户端的业务逻辑。
阅读全文