Socket Chat Client-Server Part 1Add Comment
<div align="center"> <table cellpadding=1 cellspacing=2 width="75%" class="outline" > <tr> <td width="50%" class="outline"><b>Download</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"> <a class="wbox" href="../../file/socketchat.zip">socketchat.zip</a> (41kb)</td> <td width="50%" class="outline">Beta2</td> </tr> </table> </div> <p><span class=wboxheado>Introduction</span><br> Its been quiet some time know I was looking for a good Chat Client-Server implementation. I had written several prototypes but I wasn't satisfied with the performance and the BUG's I was having in my implementations. Recently I came across a example written by <b>Mr. Rockford Lhotka</b> (Magenic Technologies) on MSDN in VB.NET. <br> I liked this implementation very much so I decided to convert and extend this example further in C#. I took me some time to convert it into C# and even more time to get used to Asynchronous Programming. But finally I guess it was really worth it, to put in the time to understand Asynchronous Programming since its very powerful and resource saving too!!</p> <p>The current version I have written has some bug's and is limited features wise, but still I am releasing the source so that while I am upgrading some features even you can test the application out and provide your feedback!</p> <p><span class=wboxheado>Known Bugs</span><br> 1) When the client hit's the 'Disconnect' button the Server does not disconnect the client, its only when you close the client the server disconnects the client.</p> <p><span class=wboxheado>Screen Shots</span></p> <p><img border="0" src="../../img/socketchatclient1.gif" width="371" height="94"><br> <b>Figure 1:</b> Socket Chat Client</p> <p><img border="0" src="../../img/socketchatserver1.gif" width="158" height="160"><br> <b>Figure 2: </b>Socket Chat Server</p> <p><span class=wboxheado>Explanation</span><br> You can get the detailed explanation by examining the inline comments in the source code available for download. Here I will just outline the overview of how the application works. <p><span class=wboxhead>Socket Chat Server</span><br> 1) Once the socket chat server is started, it starts listening for new clients on port <b>5151</b> and <b>localhost</b> as the IP. The process of accepting new clients is put within a thread.<br> 2) As soon as a new client connects, a new object of the class <b>Client </b>is made and the reference to this socket is passed as an constructor parameter to the <i>Client</i> class. The <i>Client</i> class takes the job here of handling all the communication with the connected client.<br> 3) Also the <i>Server</i> registers with the <i>Client </i>object to receive notification when the client Connects, Disconnects or sends a Message.<br> 4) As soon as the <i>Client</i> object is created it waits for the connecting user to send the <b>user name</b>. Once the user name is received it looks-up the <b>ClientList</b> class to see if the user name already exists, if so then a necessary response is given to the client and the connection is closed.<br> Incase the <i>user name</i> is unique it is added to the <i>ClientList</i> class and a special <b>GUID</b> is assigned to the user. Lastly the user is sent the list of all connected users and the <b>Connected</b> event is raised, which the <i>Server </i>picks-up and notifies all the connected users the arrival of a new client.<br> 5) One important thing to note here is that I am using <b>Asynchronous</b> method of waiting for data from clients. The benefit of this is that server consumes<i> less resources</i> and there is no need for starting a separate thread for each client! This is the reason why I have not used separate threads for each client, since Asynchronous programming takes care of the resources itself!!<br> 6) When a user sends a message the <b>MessageReceived</b> event is raised, the <i>Server Form</i> which takes care of this event, loops through the <b> Hashtable</b> of clients and sends the message to each of the clients.<p><span class=wboxhead>Socket Chat Client</span><br> 1) When the user enters his <b>user name</b> and click's the <b>Connect</b> button, the Client Form connects to the <i>Socket Chat Server </i>at port <b> 5151</b> and <b>localhost </b>and sends the <i>user name</i> to the server. (Incase you are planning to use the Client over the network you should change "localhost" to the name of the machine that will host the server).<br> 2) If the user name is unique then the server sends the unique <i>GUID</i> along with the list of connected users to the <i>Client</i>. The <i>GUID</i> is stored for future communication with the server, while the users list is added to the <b>ListBox</b>.<br> 3) Once the above step if fulfilled, the waits for messages <b>asynchronously</b>, and if the user enters a new message then its sent to the server.<br> 4) The server sends all commands (like announcement of a new user connecting) <b> prefixed</b> with the<i> GUID</i> that has been assigned to the user. So when ever a message is received, the <i>Client</i> checks if its prefixed with the <i> GUID</i>, if not it is displayed as a message.<br> Incase the message is a command i.e. it is prefixed with the GUID then the appropriate action is taken.