TCP is handy for simple, reliable communications like this tiny toy logger. I run the server and clients in separate consoles on the same machine:
# TCP log server nc -kl 8000 > server-log.txt # TCP logging from netcat client date | nc 127.0.0.1 8000 # TCP logging from socat client date | socat STDIN TCP:localhost:8000 # TCP logging from Bash client date > /dev/tcp/127.0.0.1/8000
The only bummer about TCP is that–in my example–other clients have to wait in line. We are logging so I want fast, one-way communication from any number of clients to the server, and reliability of every log message is probably not critical. Let’s try UDP! I could just add -u to the netcat server args to use UDP datagrams, but a netcat UDP server gets a little wonky. The easy workaround is to use socat as the server instead. socat happily accepts any datagram from multiple clients, simultaneously.
# UDP log server socat UDP-RECV:8000 STDOUT > server-log.txt # UDP logging from netcat client date | nc -q1 -4 -u 127.0.0.1 8000 # UDP logging from socat client date | socat STDIN UDP-DATAGRAM:localhost:8000 # UDP logging from Bash client date > /dev/udp/127.0.0.1/8000
Use at your own risk. The TCP version is surely simplest, safest (ahem, still no auth – this is just a toy) and reliable. I don’t know much about what’s going on under the hood here. Insight welcome! Messages from different clients might get mangled together, too. Tested on Ubuntu 14.04.