庆幸的是借助getty良好的设计模式,getty udp的使用方式与tcp差别不大。

    1. 唯一的区别就是需要指定socketChannelUDP。默认不指定是TCP

    需要指定UDP的消息编解码器

    1. //对于UDP而言,只有DatagramPacketEncoder、DatagramPacketDecoder两个处理器是有效的
    2. //原因在于UDP本身不存在连接,因而其他处理器即使配置了也是没有效果的
    3. defaultChannelPipeline.addLast(new DatagramPacketEncoder());
    4. defaultChannelPipeline.addLast(new DatagramPacketDecoder());
    1. //服务器端
    2. AioServerStarter server = new AioServerStarter(8888);
    3. //唯一的区别就是需要指定socketChannel为UDP。默认不指定是TCP
    4. server.socketChannel(SocketChannel.UDP).channelInitializer(new ChannelInitializer() {
    5. @Override
    6. public void initChannel(AioChannel channel) throws Exception {
    7. //获取责任链对象
    8. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    9. //对于UDP而言,只有DatagramPacketEncoder、DatagramPacketDecoder两个处理器是有效的
    10. //原因在于UDP本身不存在连接,因而其他处理器即使配置了也是没有效果的
    11. defaultChannelPipeline.addLast(new DatagramPacketEncoder());
    12. defaultChannelPipeline.addLast(new DatagramPacketDecoder());
    13. //添加自定义的简单消息处理器
    14. defaultChannelPipeline.addLast(new SimpleHandler());
    15. }
    16. }).start();
    17. ------------------------------------------------------------------
    18. //客户端
    19. AioClientConfig aioConfig = new AioClientConfig();
    20. //Host与port无需指定也没有关系,指定了也没有效果
    21. aioConfig.setHost("127.0.0.1");
    22. aioConfig.setPort(port);
    23. aioConfig.setClientChunkSize(512 * 1024 * 1024);
    24. aioConfig.setBufferWriterQueueSize(2 * 1024 * 1024);
    25. AioClientStarter client = new AioClientStarter(aioConfig);
    26. client.socketChannel(SocketChannel.UDP).channelInitializer(new ChannelInitializer() {
    27. @Override
    28. public void initChannel(AioChannel channel) throws Exception {
    29. //责任链
    30. DefaultChannelPipeline defaultChannelPipeline = channel.getDefaultChannelPipeline();
    31. defaultChannelPipeline.addLast(new DatagramPacketEncoder());
    32. defaultChannelPipeline.addLast(new DatagramPacketDecoder());
    33. //定义消息解码器
    34. defaultChannelPipeline.addLast(new SimpleHandler());
    35. }
    36. });
    37. try {
    38. client.start();
    39. } catch (Exception e) {
    40. e.printStackTrace();
    41. }
    42. //发送个消息给服务器
    43. Thread.sleep(2000);
    44. AioChannel aioChannel = client.getAioChannel();
    45. String s = "hello getty";
    46. byte[] msgBody = s.getBytes("utf-8");
    47. //UDP消息发送的是DatagramPacket 发送的目标服务器在DatagramPacket 中指定
    48. DatagramPacket datagramPacket = new DatagramPacket(msgBody, msgBody.length, new InetSocketAddress("127.0.0.1", 8888));
    49. aioChannel.writeAndFlush(datagramPacket);

    SimpleHandler的类型指定为 > DatagramPacket

    1. public class SimpleHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    2. @Override
    3. public void channelAdded(AioChannel aioChannel) {
    4. System.out.println("连接成功");
    5. }
    6. @Override
    7. public void channelClosed(AioChannel aioChannel) {
    8. System.out.println("连接关闭了");
    9. }
    10. @Override
    11. public void channelRead0(AioChannel aioChannel, DatagramPacket datagramPacket) {
    12. System.out.println("读取消息了:" + new String(datagramPacket.getData()));
    13. System.out.println("客户端地址:" + datagramPacket.getAddress().getHostName() + ":" + datagramPacket.getPort());
    14. }
    15. @Override
    16. public void exceptionCaught(AioChannel aioChannel, Throwable cause, PipelineDirection pipelineDirection) {
    17. System.out.println("出错了");
    18. }
    19. }