echo server with netcat or socat

Solution 1:

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

Solution 2:

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!


Solution 3:

You can also use socat (rather than using netcat) as echo server and netcat as client.

Socat echo server (listens on TCP port 1234):

socat -v tcp-l:1234,fork exec:'/bin/cat'

Netcat client (connects to serverip on TCP port 1234):

nc serverip 1234

Solution 4:

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.