Socket Chat Client-Server Part 1
Add Comment| 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.

