|
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.
Following table shows the internal messages used in chat server
|
|
|
|
<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 |
|
|
|
|
<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. |