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.
22 Answers
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).
If supported, use reuseaddr with both commands. They will be like
echo <packetContent> | socat - udp:<dstIP>:<dstPort>,sp=<srcPort>,reuseaddr
socat - udp-listen:<srcPortFromPreviousLine>,reuseaddrFrom 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 bysocat.