Netty的基本使用

Netty的基本使用

服务端定义

开启服务端端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class MyServer {
public static void main(String[] args) {
EventLoopGroup boseGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
// handler 只针对boseGroup
// childHandler 经过boseGroup后再经过workerGroup
serverBootstrap.group(boseGroup,workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new MyServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();


}catch (Exception e) {
e.printStackTrace();
}finally {
boseGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}

定义服务端initialize

1
2
3
4
5
6
7
8
9
10
11
public class MyServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyHttpServerHandler());
}
}

定义服务端handler

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyHttpServerHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(ctx.channel().remoteAddress() + ", " + msg);
ctx.channel().writeAndFlush("from server "+ UUID.randomUUID());
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}

客户端定义

绑定客户端端口和ip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MyClient {
public static void main(String[] args) {
EventLoopGroup eventExecutors = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventExecutors).channel(NioSocketChannel.class)
.handler(new MyClientInitializer());
ChannelFuture localhost = bootstrap.connect("localhost", 8899).sync();
localhost.channel().closeFuture().sync();

}catch (Exception e) {
e.printStackTrace();
}finally {
eventExecutors.shutdownGracefully();
}
}
}

定义客户端initialize

1
2
3
4
5
6
7
8
9
10
11
public class MyClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,0,4,0,4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new MyClientHandler());
}
}

定义客户端handler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class MyClientHandler extends SimpleChannelInboundHandler<String> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(ctx.channel().remoteAddress());
System.out.println("client ouput: " + msg);
ctx.channel().writeAndFlush("from client:"+ LocalDateTime.now());
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush("来自于客户端的问候!");
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}