Wednesday, July 3, 2013

Simple Chat Program in C (Part 3)

Read Part 2 : The Client

Sample Execution:

$ gcc server.c -o server -lpthread
$ gcc client.c -o client -lpthread

Now we run the server program and several client programs

Each client should first connect to the server by typing in:

login [alias]
Here [alias] is optional, if it is not mentioned, the name will be set as Anonymous.

The name can be changed by the command:

alias [new_name]
After this, you alias will be changed to the new name you have given.

To send a message to everyone connected at the moment:

send [message]

To send a message to a specific alias:

whisp [user_alias] [message]
If the target alias is not active at that moment, message will not be sent anywhere

You can disconnect from the server by typing:

You can again reconnect by using the login command

You can terminate the client by typing:


This is pretty much of it. Basically this is good for your C lab assignments regarding TCP / socket based chat clients. Here is a screen shot from my computer:

Good luck!


  1. hi zobayer, i learn coding from your spoj solutions :)

  2. I Know It's not the place to ask you this question here. But i really have a great problem in one of my project where i have to print a text file(*.txt) by a printer through C coding. Can you provide me some reference or instruction ?

    1. see fopen(), fwrite(), fprintf() etc from
      for low level operations open() and read() / write() can help

    2. Brother i am not asking you about file i/o system. May be i could not ask you the question properly at previous time. I want to access a peripheral or USB port Or may be the driver file in the system of the OS(windows) of my pc so that i can give instruction to the output device (such as printer) to print a text(*.txt) file. I really need some reference.

    3. oh sorry, for windows, can't help, however in linux its pretty straight forward. you can get plenty of results by googling... good luck.

  3. hey bro , can u pls expalain me the solution of LIS2 in spoj
    Waiting for your reply

  4. Hi zobayer, l learn coding from ur spoj solution. but it seems that you have removed ur solution from GitHub. Have u posted them on other site?

    1. Hi, no, I didn't post them in any other site, I have taken those away on request, because people were abusing them too much. There are problems where 90% of the top 30 submissions were exactly my solutions. So I think instead of taking help, people are using those as a means of racking up their score. I also got a mail from spoj admin Łukasz Kuszner regarding this issue. I hope you will understand. If you really need to see a solution that I have solved, send me an email, I will send you the source code if I feel appropriate.
      Thanks for understanding :)

  5. Hi All,

    Can Any one provide the Chat pgm in C-Language (Ex: Happening b/w two users ), thanks in advance and great appreciate if u provide the solution!

    Thanks & Regards,
    Ravi k.

  6. thanks god i finally finish my IT homework :D thank you a lot !!!

  7. After exit command in /.server, it exits well but when I rewrite ./server again it shows this "bind() failed..." in terminal. Can you help me please ???

  8. That is because the port is still active with the process. Make sure you release the port. You can check which ports are currently in use by using netstat command.