0

我用 UdpClient、UdpServer、Spring Integration Ip 模块尝试了很多东西,包装了 DatagramSocket 接收方法以返回 Flux,但我根本无法从标准 SSDP 239.255.255.250:1900 收到任何响应。

还将数据包发送到 SSDP 的奖励积分。

4

1 回答 1

0

对于那些感兴趣的人,这里有一个片段。我能够通过 Flux 发射器而不是 reactor-netty UDP 类来做到这一点。

private static final String SSDP_IP = "239.255.255.250";
private static final int SSDP_PORT = 1900;
private static final int TIMEOUT = 5000;

String request = new String("example");
byte[] receiveData = new byte[1024];
DatagramPacket sendPacket = new DatagramPacket(request.getBytes(),
            request.getBytes().length, InetAddress.getByName(SSDP_IP), SSDP_PORT);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length,
            InetAddress.getLocalHost(), SSDP_PORT);

Flux<String> response = Flux.create(emitter -> {
    try (DatagramSocket socket = new DatagramSocket()) {
        socket.setSoTimeout(TIMEOUT);
        socket.send(sendPacket);
        while (true) {
            socket.receive(receivePacket);
            emitter.next(receivePacket.getData());
        }
    } catch (SocketTimeoutException e) {
        emitter.complete();
    } catch (IOException e) {
        emitter.error(e);
    }
})
    .map(mapper -> new String(receivePacket.getData(), 0, receivePacket.getLength()))
于 2018-12-10T04:24:27.963 回答