How to build a simple chat using netcat?

You can do something like this.

Assume Alice is the server. She types:

mawk -W interactive '$0="Alice: "$0' | nc -l -p <port_number> <ip_of_alice>

Then Bob connects to that server. He types:

mawk -W interactive '$0="Bob: "$0' | nc <ip_of_alice> <port_number>

The mawk lines just adds the prepending name of the person to the "chat". We need -W interactive to set unbuffered writes to stdout and line buffered reads from stdin.


Now Alice types Hi Bob and sees:

Hi Bob

Bob sees:

Alice: Hi Bob

Bob types Hi Alice and sees:

Alice: Hi Bob
Hi Alice

Alice sees:

Hi Bob
Bob: Hi Alice