0
votes

I tried a lot of stuff with UdpClient, UdpServer, Spring Integration Ip module, wrapping DatagramSocket receive method to return a Flux, but I simply cannot receive any response from standard SSDP 239.255.255.250:1900.

Bonus points for also sending a packet to SSDP.

1

1 Answers

0
votes

For those interested, here's a snippet. I was able to do it via Flux emitter instead of reactor-netty UDP classes.

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()))