netcat is a wonderful utility for those who regularly do network programming. I find myself using netcat to avoid writing code to do simple network IO. Here I document some of the common uses I put it to. A similar replacement utility called ncat is available with nmap, it also works on Windows. The version of netcat used in the examples below is the OpenBSD version distributed by Ubuntu.
Send or Receive data using UDP
netcat -u 127.0.0.1 8001
Any data you type into
stdin is sent to port
8001 of the
localhost. You can start another instance of
netcat with the command line shown below to receive the data.
netcat -l -u 127.0.0.1 8001
This can be used to establish a simple UDP based chat mechanism.
Send or Receive data using TCP
The UDP chat example in the previous section can be implemented in TCP by dropping the
-u argument from the arguments. TCP is the default mode. This being TCP, the receiver end has to be started first (the server) and then the sender (the client).