Send UDP packet and listen for replies

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.

2

2 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>,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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like