29

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?

  • 1
    nc don't known username and can not send it. Only can do is to type message in format Bob: hello. – 2707974 Aug 25 '15 at 6:28
  • I want to avoid that exact thing . I am also open to alternate programs that can achieve the same functionality. – Sachin Kamath Aug 25 '15 at 6:32
  • Try to find solution in this answer – 2707974 Aug 25 '15 at 6:51
36

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
|improve this answer|||||
  • 2
    nc cannot use -s and -l at the same time. the -s is not required. – Sachin Kamath Aug 25 '15 at 11:03
  • I got awk: option `-W interactive' unrecognized, ignored – zt1983811 Jul 4 '16 at 16:03
  • @zt1983811 you need GNU awk, not mawk. – chaos Jul 4 '16 at 17:57
  • @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 – zt1983811 Jul 4 '16 at 20:03
  • 1
    @zt1983811 This is a mawk option, not gawk. Did you try it using mawk explicitly? – dessert Sep 15 '17 at 6:59
0

A bit ago, I actually made one program that enables chatting (via IP address) on lan, there must be errors I didn't really had time to maintain it but it should give you something to start.

https://github.com/Aeres-u99/ncChat

PS: I had tried adding a file sharing support as well :P

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.