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
Related videos on Youtube
![Sachin S Kamath](https://i.stack.imgur.com/onoXY.jpg?s=256&g=1)
Sachin S Kamath
Security researcher. Red Teamer. Open Source Sorcerer. He/Him.
Updated on September 18, 2022Comments
-
Sachin S Kamath almost 2 years
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?-
2707974 almost 9 years
nc
don't knownusername
and can not send it. Only can do is to type message in formatBob: hello
. -
Sachin S Kamath almost 9 yearsI want to avoid that exact thing . I am also open to alternate programs that can achieve the same functionality.
-
2707974 almost 9 yearsTry to find solution in this answer
-
-
Sachin S Kamath almost 9 yearsnc cannot use -s and -l at the same time. the -s is not required.
-
zt1983811 almost 8 yearsI got awk: option `-W interactive' unrecognized, ignored
-
chaos almost 8 years@zt1983811 you need GNU
awk
, notmawk
. -
zt1983811 almost 8 years@chaos I did use awk awk -W interactive '$0="Alice: "$0' | nc -lvk ip port ; ls -al /usr/bin/awk lrwxrwxrwx 1 root root 21 Jun 27 06:48 /usr/bin/awk -> /etc/alternatives/awk
-
chaos almost 8 years@zt1983811 default is mawk, use
apt-get install gawk
to install GNU awk. awk will then point to GNU awk. -
zt1983811 almost 8 years@chaos I do not think it is still working, I even tried following: gawk -W interactive '$0="Alice: "$0' | nc -lvk 10.0.5.30 4000 Listening on [10.0.5.30] (family 0, port 4000) gawk: option `-W interactive' unrecognized, ignored
-
dessert almost 7 years@zt1983811 This is a
mawk
option, notgawk
. Did you try it usingmawk
explicitly?