Send UDP packet and listen for replies

0

I have a program with a Send an UDP packet to port xyz and I will reply with some UDP packets! interface.

I've found out that I can send a UDP packet with

echo <packetContent> | socat - udp:<dstIP>:<dstPort>,sp=<srcPort>

and listen (= just dump their content to stdout) for UDP packets with

socat - udp-listen:<srcPortFromPreviousLine>

But how can I bring those two together? When I start the listening part in one window at first, the port is already in use and I cannot send anything from there in another window.

Bowi

Posted 2019-08-21T15:15:12.597

Reputation: 995

1Isn't udp: already bidirectional? – user1686 – 2019-08-21T15:55:46.877

Thanks a lot! Brought me to the solution. =) – Bowi – 2019-08-22T09:38:05.153

Answers

1

Like grawity said in the comment, udp: is bidirectional. So I can just use one command:

echo <packetContent> | socat -t 10 - udp:<dstIp>:<dstPort>,sp:<srcPort>

This sends the packet and prints any packets coming back from there; if none arrive, it quits after 10 seconds (-t 10).

Bowi

Posted 2019-08-21T15:15:12.597

Reputation: 995

0

If supported, use reuseaddr with both commands. They will be like

echo <packetContent> | socat - udp:<dstIP>:<dstPort>,sp=<srcPort>,reuseaddr
socat - udp-listen:<srcPortFromPreviousLine>,reuseaddr

From man 1 socat:

reuseaddr
Allows other sockets to bind to an address even if parts of it (e.g. the local port) are already in use by socat.

Kamil Maciorowski

Posted 2019-08-21T15:15:12.597

Reputation: 38 429