创建handler

public class DemoUDPHandler implements MagicianUDPHandler {

    @Override
    public void receive(ByteArrayOutputStream byteArrayOutputStream) {
        try {
            String str = byteArrayOutputStream.toString("UTF-8");
            System.out.println(str.length());
            System.out.println(str);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

创建UDP服务

Magician.createUdpServer()
        .threadPool(Executors.newCachedThreadPool()) // 设置线程池,非必填,默认Executors.newCachedThreadPool()
        .readSize(65507) // 缓冲区大小,建议跟报文长度一致,非必填,默认: 1024
        .handler(new DemoUDPHandler()) // handler,必填
        .bind(8088) // 端口号,必填

results matching ""

    No results matching ""