Multiple Clients Chat Server


This program deals with communication between two clients (one to one) and between a single user and multiple clients, commonly known Broadcasting.

This is a dialog-based application. We have harnessed the socket functionality by deriving the mysocket class from CSocket which is wrapper class in MFC for socket functions. The project is divided into two modules : server and client

In server and client both CChatSocketDlg is the class wrapping the user interface components. Server has the mysocket object named m_listensocket which listens for the incoming client connections at the port specified in UI. Client has m_connectsocket to carryout the connection with server. When a client requests connection, the mysocket::OnAccept( ) is invoked which in turn invokes the CChatsocketDlg::onaccept( ). The connection is accepted by listensocket by calling Accept( ) member function of CAsyncsocket which is base class of CSocket which in turn is base class of mysocket.

After connection has established client sends the validation packet containing the <UID>username and <PWD>password for checking authenticity of already signed up user or <NUID>username and <NPWD>password for a new user who wished to signup. Server checks the authenticity of a registered user by invoking CChatsocketDlg::validateuser( ) or for new user calls CChatsocketDlg::registeruser( ). It then returns <VALID> or <REGISTERED> to notify client about successful validation or <INVALID> or <TRYAGAIN> to notify that user is not authenticated. The client may try again for validation if not able to validate earlier. After the client gets validated the socket corresponding to client on server is added to collection of client and this newly joined client is notified about all other online clients using message notation <USERJOINED>username. All other clients are also informed about joining of new client. The client add the username provided by the Server through message <USERJOINED>username in user’s online list.

The client can now send the message to two type of recipients.

  1. Multicasting or Broadcasting: In this as mentioned in introduction the message typed by client is sent to all the users online. This is default mode of sending message. In this mode the message is sent to server without any enclosement of internal messages. Any message received in this form is relayed to all clients in the collection through the collection of server side client socket objects maintained by server. It is done by invoking CChatsocketDlg::Broadcast from mysocket::OnReceive member function with the help of mysocket::m_dlg pointer.
  2. Unicasting: In this the message recipient is only one user specified by client. It is a sort of private messaging from one client to another. In this the message is wrapped with <FORUSER>recipientusername<MSG>message and then sent to server. Server checks for wrapper and then forwards the message to recipient user specified in the message by invoking CChatsocketDlg::sendindmessage( ) with the help of mysocket::m_dlg pointer. When the client disconnects it sends the Server message <DISCONNECTING>username and then closes the socket connection with server. Server relays message to all other online clients that username has left and then removes corresponding socket from collection of online client sockets by invoking CChatsocketDlg::disconnectuser( ) with the help of mysocket::m_dlg pointer.

Following table shows the internal messages used in chat server

Message

Parameters

Description

<UID>

Uname

Indicates uname is user ID to be validated

<PWD>

Pwd

Indicates pwd is password for corresponding userID to be validated

<DISCONNECTING>

---

It indicates that the client is disconnecting from the server

<FORUSER>

Uname

It indicates the destination client for whom the message is meant.

<MSG>

Message

Message to be sent to client uname only

 

Message

Parameters

Description

<VALID>

 

Valid username and password

<INVALID>

 

Invalid username or password

<REGISTERED>

 

User Registered

<TRYAGAIN>

 

User already exists

<USERJOINED>

Uname

Name to be added in User’s online list.

<USERLEFT>

Uname

Name to be removed from User’s online list.

 

Download