compile group: 'io.netty', name: 'netty-all', version: '4.1.42.Final'
package com.test.udp; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioDatagramChannel; public class NettyServer { private final int port; public NettyServer(int port) { this.port = port; } public void start() throws Exception { EventLoopGroup bossGroup=new NioEventLoopGroup(); try { //通过NioDatagramChannel创建Channel,并设置Socket参数支持广播 //UDP相对于TCP不需要在客户端和服务端建立实际的连接,因此不需要为连接(ChannelPipeline)设置handler Bootstrap b=new Bootstrap(); b.group(bossGroup) .channel(NioDatagramChannel.class) .option(ChannelOption.SO_BROADCAST, true) .handler(new ServerHandler()); b.bind(port).sync().channel().closeFuture().await(); } catch (Exception e) { e.printStackTrace(); } finally{ bossGroup.shutdownGracefully(); } } }
启动类
public class ThumbnailServiceApplication { public static void main(String[] args) throws Exception { SpringApplication.run(ThumbnailServiceApplication.class, args); new NettyServer(12345).start(); } }