UDPEchoServer.java 1.0 KB

123456789101112131415161718192021222324252627282930
  1. package com.xiaobao.gateway.protocol.udp;
  2. import io.netty.bootstrap.Bootstrap;
  3. import io.netty.channel.Channel;
  4. import io.netty.channel.ChannelOption;
  5. import io.netty.channel.EventLoopGroup;
  6. import io.netty.channel.nio.NioEventLoopGroup;
  7. import io.netty.channel.socket.nio.NioDatagramChannel;
  8. public class UDPEchoServer {
  9. static final int PORT = Integer.parseInt(System.getProperty("port", "8006"));
  10. public static void main(String[] args) {
  11. EventLoopGroup group = new NioEventLoopGroup();
  12. try {
  13. Bootstrap bootstrap = new Bootstrap();
  14. bootstrap
  15. .group(group)
  16. .channel(NioDatagramChannel.class)
  17. .option(ChannelOption.SO_BROADCAST, true)
  18. .handler(new ClientProxyHandler());
  19. Channel channel = bootstrap.bind(PORT).sync().channel();
  20. channel.closeFuture().sync();
  21. } catch (Exception e) {
  22. System.out.println(e.getMessage());
  23. } finally {
  24. group.shutdownGracefully();
  25. }
  26. }
  27. }