I want a UDP echo server to get packets, and reply exactly what it has received. How can I simply do this using netcat or socat? It should stay alive forever and handle packets coming from several hosts.

link|improve this question
Please don't make this thing Internet-accessible. Since it's UDP-based it can be used to send a packet stream to arbitrary destinations using source-forged packets. – Evan Anderson Jan 6 at 5:47
feedback

3 Answers

up vote 2 down vote accepted

Another netcat-like tool is the nmap version, ncat, that has lots of built in goodies to simplify things like this. This would work:

ncat -e /bin/cat -k -u -l 1235

-e means it executes /bin/cat (to echo back what you type)
-k means keep-alive, that it keeps listening after each connection
-u means udp
-l 1235 means that it listens on port 1235

link|improve this answer
feedback

You can write a C program that forks nc -u -l -p 4321 and then uses dup(2) to connect:

  • nc stdin with the parent's stdout
  • nc stdout with the parent's stdin

Then in an endless loop the parent reads from stdin and writes in stdout whatever the parent reads.

link|improve this answer
I can use socat -v PIPE UDP-LISTEN:5555,fork, but the second connection gets refused. By the way, it works fine with TCP-LISTEN. I am avoiding to code, otherwise I could write it all in C. – Mohammad Hedayati Jan 4 at 13:16
You have to wrap the whole thing in an endless loop: while true; do socat stuff; done – adamo Jan 4 at 18:53
You may also get some inspiration using this tutorial – adamo Jan 4 at 19:16
feedback

I used socat -v PIPE udp-recvfrom:5553,fork to run the server and socat - udp:localhost:5553 for clients. This was a great help!

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.