目标
之前的章节我们学习了编码服务端,使用TCP通信,本章我们学习如何使用Netty构建一个UDP应用。
创建UDP Server
package com.coman404.base;
import com.coman404.base.handler.UDPServerHandler;
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.nio.NioDatagramChannel;
import java.util.Objects;
public class UDPServer {
public static void main(String[] args) {
int port = 8081;
EventLoopGroup group = new NioEventLoopGroup();
try{
Bootstrap server = new Bootstrap();
server.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(new UDPServerHandler());
}
});
ChannelFuture f = server.bind(port).sync();
f.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
}
注意设置的channel是NioDatagramChannel类
创建ServerHandler
public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg){
ByteBuf byteBuf = msg.content();
int len = byteBuf.readableBytes();
byte[] bytes = new byte[len];
byteBuf.readBytes(bytes);
System.out.println(new String(bytes,StandardCharsets.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
super.exceptionCaught(ctx, cause);
}
}
UDP接收的是数据报文DatagramPacket
创建UDP客户端
public class UDPClient {
public static void main(String[] args){
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline().addLast(new UDPClientHandler());
}
});
ChannelFuture f = bootstrap.connect("localhost",8089).sync();
if (f.isSuccess()){
System.out.println("连接成功");
}
Channel channel = f.channel();
channel.writeAndFlush(new DatagramPacket(channel.alloc().buffer().writeBytes("hello world".getBytes(StandardCharsets.UTF_8)),new InetSocketAddress("localhost",8089)));
channel.closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
group.shutdownGracefully();
}
}
}
创建客户端的Handler
public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
ByteBuf byteBuf = msg.content();
int len = byteBuf.readableBytes();
byte[] bytes = new byte[len];
byteBuf.readBytes(bytes);
System.out.println(new String(bytes,StandardCharsets.UTF_8));
}
}
测试UDP 的客户端和Server端时,不要在同一个机器上,因为UDP是无连接的,他们都连接在同一端口上,测试数据没有响应