www.MasterCsharp.com Logo  AksTech Ad
 Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 

 

Socket Chat Client-Server Part 1
   
 

 

[Rate this Article]
Download SDK
socketchat.zip (41kb) Beta2

Introduction
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 Mr. Rockford Lhotka (Magenic Technologies) on MSDN in VB.NET.
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!!

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!

Known Bugs
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.

Screen Shots


Figure 1: Socket Chat Client


Figure 2: Socket Chat Server

Explanation
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.

Socket Chat Server
1) Once the socket chat server is started, it starts listening for new clients on port 5151 and localhost as the IP. The process of accepting new clients is put within a thread.
2) As soon as a new client connects, a new object of the class Client is made and the reference to this socket is passed as an constructor parameter to the Client class. The Client class takes the job here of handling all the communication with the connected client.
3) Also the Server registers with the Client object to receive notification when the client Connects, Disconnects or sends a Message.
4) As soon as the Client object is created it waits for the connecting user to send the user name. Once the user name is received it looks-up the ClientList 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.
Incase the user name is unique it is added to the ClientList class and a special GUID is assigned to the user. Lastly the user is sent the list of all connected users and the Connected event is raised, which the Server picks-up and notifies all the connected users the arrival of a new client.
5) One important thing to note here is that I am using Asynchronous method of waiting for data from clients. The benefit of this is that server consumes less resources 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!!
6) When a user sends a message the MessageReceived event is raised, the Server Form which takes care of this event, loops through the Hashtable of clients and sends the message to each of the clients.

Socket Chat Client
1) When the user enters his user name and click's the Connect button, the Client Form connects to the Socket Chat Server at port 5151 and localhost and sends the user name 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).
2) If the user name is unique then the server sends the unique GUID along with the list of connected users to the Client. The GUID is stored for future communication with the server, while the users list is added to the ListBox.
3) Once the above step if fulfilled, the waits for messages asynchronously, and if the user enters a new message then its sent to the server.
4) The server sends all commands (like announcement of a new user connecting) prefixed with the GUID that has been assigned to the user. So when ever a message is received, the Client checks if its prefixed with the GUID, if not it is displayed as a message.
Incase the message is a command i.e. it is prefixed with the GUID then the appropriate action is taken.

 

  
Saurabh Nandu - 11 September 2001


Your Ratings / Comments
     
 

[Go to Top]

How many cups of coffee is this article worth??

Rating (Bad)-(Excellent)

Your Name
Your E-mail  
Your Message (Optional)

Viewer Ratings/Comments
Rating Description
Just Comments - jassi on 6/8/2002 11:03:00 AM
1 - rajiv jain on 7/12/2002 7:49:00 AM
5 THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS THANKS - UFUK ACAR on 8/20/2003 6:17:00 AM
1 sfhsdhs - fdhgsfdgh on 8/30/2003 11:06:00 AM
1 dsfsdfsdfdsfsdfsdf - dxfdsfdf on 9/22/2003 5:55:00 AM
5 - dragan on 10/10/2003 12:55:00 PM
5 12312312312 - 123 on 11/4/2003 8:37:00 PM
4 nice! - aov on 2/9/2005 12:17:00 PM
4 good one - Rajeshwari on 8/2/2005 7:21:00 AM
3 Missis code snip :) - Digital Human on 8/3/2005 8:01:00 PM
3 good!!!!!! - puckdee on 8/24/2005 6:10:00 PM
5 Best code on the net! - josef on 3/10/2006 11:03:00 AM
3 i am the student of MCS Final. I have choose Chat server project using dot net. I need some help. Please send me the whole procedure - Imran TAj on 8/19/2006 10:16:00 AM
4 My name is M. Imran Taj i am the student of MCS Final. I have choose Chat server project using dot net. I need some help. Please send me the whole procedure - loving_2me@yahoo.co.uk - Imran Taj on 8/19/2006 10:41:00 AM
1 Thank you. - Pankaj on 1/20/2007 1:43:00 AM
4 great - Altaf Hussain on 2/8/2007 7:52:00 AM
Just Comments I need the chat server project with coding. Pls send it to me asap. - karikalan on 3/6/2007 1:59:00 AM
4 I ran your application. But, it only accepts a client to be connected. It's unlike the screen shots you took which allow multiple clients to communicate with each other. - cheryl on 5/21/2007 8:18:00 AM
1 i'm student. i 'm researching about the connection betwen computer and microcontroller (PIC18F67J60). apart of my role is programming for computer. and i think your program can help for my project.thank you very much - nguyen ngoc huan on 7/31/2007 9:34:00 AM
Just Comments I can not run that download data - Soniya on 9/13/2007 1:53:00 AM
4 tat was cool - xyz on 1/8/2008 2:14:00 AM
5 asdasdsad asd as dasd as das a das - rojeh on 2/11/2008 7:03:00 PM
4 Well Done!! - Essam on 2/18/2008 3:15:00 AM
1 Хуйня редкая, даже чат нормальный сделать не можете - ArhAngel01 on 3/28/2008 8:18:00 AM
3 AsalamoAlikum(Its a pray to u)Means>>>> May Allah Peace Be On U.... Thanks U soo very Much.. - Aman on 4/17/2008 2:06:00 AM
Just Comments Excellent, i am also working on a GPRS messaging server in C#, my problem is that not more than approx 200 clients connect to my server, can u help - Yasir Akram on 4/29/2008 3:23:00 AM
5 Excellent, i am also working on a GPRS messaging server in C#, my problem is that not more than approx 200 clients connect to my server, can u help - Yasir Akram on 4/29/2008 3:52:00 AM
5 Your code is excellent. I`m writting a code to connect several radio frequency readers to a server and your idea is exellenent for this. Please, can you send me the full procedure ? - Pelope Ad on 6/25/2008 12:14:00 PM
Just Comments ddddddddddd - fffffffffff on 6/28/2008 3:10:00 PM
Just Comments ddddddddddd - fffffffffff on 6/28/2008 3:10:00 PM
Just Comments Хуйня редкая, даже чат нормальный сделать не можете - ArhAngel01 on 3/28/2008 8:18:00 AM )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) - bob on 7/8/2008 10:39:00 AM
3 You should invoke form components. Otherwise there will always be some cross-thread calls - Ikado on 11/4/2008 1:06:00 AM
5 - Lee on 11/5/2008 6:34:00 PM
5 Excellent, could u send me the source code? - Ba Vi on 4/6/2010 9:41:00 PM
2 bbbbb - atball on 4/24/2010 3:42:00 AM
2 - Juhasz Eszter on 4/29/2010 11:05:00 AM
Just Comments Have you even test your application for one time ? server startup raise error (cross thread) - Ehsan on 7/27/2010 1:19:00 PM
3 thank you for best project - xaniar on 8/23/2010 10:43:00 PM

[Go to Top]


 
 
  Copyright © 2002 - 2004 MasterCSharp.com. All rights are reserved.

  Presenting MasterCSharp.com in association with AksTech Solutions - .NET Solutions Development and Consulting. 

  Best Viewed in IE 4.0+ and 800x600 Resolution