springboot+netty实现UDP的接收与发送

在springboot工程中集成netty框架实现UDP的接收与发送,话不多说,直接上代码。

1、Java中基于netty实现

    客户端向服务端发送“成语”或“谚语”时,服务端会随机生成对应的成语和谚语返回给客户端; 服务端如果接收到除“谚语”和“成语”的其他字符串则发送“请发送‘谚语’或‘成语’”的提示语;

1.1、服务端

public class UdpServer {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap
                    .group(group)
                    .channel(NioDatagramChannel.class)
                    .option(ChannelOption.SO_BROADCAST, true)
                    .handler(new UdpServerHandler());
            bootstrap
                    .bind(8082)
                    .sync()
                    .channel()
                    .closeFuture()
                    .sync();
        } finally {
经验分享 程序员 微信小程序 职场和发展