How to write a echo server bash script using tools like nc, echo, xargs, etc capable of simultaneously processing requests from multiple clients each with durable connection?
The best that I've came up so far is
nc -l -p 2000 -c 'xargs -n1 echo'
but it only allows a single connection.
netcat
solution pre-installed in UbunutuThe
netcat
pre-installed in Ubuntu 16.04 comes fromnetcat-openbsd
, and has no-c
option, but the manual gives a solution:Then client example:
TODO: how to modify the input string value? The following does not return any reply:
The remote shell example however works:
I don't know how to deal with concurrent requests simply however.
In case ncat is not an option, socat will also work:
The
fork
is necessary so multiple connections can be accepted. Addingreuseaddr
toTCP4-LISTEN
may be convenient.Here are some examples. ncat simple services
TCP echo server
UDP echo server
If you use ncat instead of nc your command line works fine with multiple connections but (as you pointed out) without -p.
ncat is available at http://nmap.org/ncat/.
P.S. with the original the Hobbit's netcat (nc) the -c flag is not supported.
Update: -k (--keep-open) is now required to handle multiple connections.