Chat in C Programming Language (server side)

(Code Below) 

I created this project to use several computers to communicate between them. This program launches a server in one IP address and one Port, and then you can connect to this server through TELNET protocol:


After all the clients being connected to the server, anyone can communicate a message. For that, a peer sends a message to the server, and the server will Broadcast that message onto every other participant:



And that's how it works. It is a system that allows you to communicate with everyone inside the network (A bit like a Hangout with just text). This is useful in case you want to create some system that is able to send commands to some computer (i.e. like sending a command from your phone/tablet to the server, to make the server open a door or start recording some video from a webcam)... etc. The possibilities are endless here. For that, you can grab this code and just focus on parsing the text and redirecting commands accordingly... the rest is done.


Some prints on how it works:

1 - Compiling and starting the server on a given port (if you don't enter the port, the default port will be 1234)




2 - Connect a client. For this you don't need to create a program client... you can use the TELNET protocol available both in unix/windows. Use the server and the port to connect to our server: 



If the connection is not ok, it will be refused and closed. If it succeedes as intended, the outcome will be a greeting.



3 - After doing it with 2 PCs (1 with Ubuntu, 1 with XP), the server tells you that it successfully linked 2 peers:


I limited the code to accept just 3 peers. To any adjustment, alter the code ;)



4 - To send a message, type it in the client and hit enter.




5 - This is the other TELNET client in windows. You start it in the same way you do in Linux (open the windows console and type:     telnet     server      port). The outcome is a window that will show every message sent from other peers. As proof, the next image shows the text typed in the Linux client.




It also works on Android (in fact, it works in any platform, as long as you have a TELNET client):




It works and is multi platform ^_^


Code:  sockets.cpp  ip.cpp  ip.h

Published by fxsf às 16:58