I am currently working on a project and I have implemented a simple chat application using the netcat
libraries.
The client is prompted to enter port number and the command
nc -l -p xxxx
where xxxx
is the port number entered by the client.
Similarly, the host is prompted for the same port number and a connection is established using
nc <ip_address> -p xxxx
However, this gives a blank chat experience as it does not show the username of the person typing the messages, something like
hey
hello
what's up
Nothing
Instead, I want it to be something like,
Foo : hey
Boo : hello
Foo : what's up
Boo : Nothing
Can I use netcat
to achieve this functionality or is there anything else that does this?
Best Answer
You can do something like this.
Assume Alice is the server. She types:
Then Bob connects to that server. He types:
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:Bob sees:
Bob types
Hi Alice
and sees:Alice sees: