Tuesday, August 20, 2013

Threaded multi Chat room client source code in c++ with MFC Part - 1

As in my previous post I have talk about server , now it's time for client. This client will use as multi chat client with our very own server. In this client you need to provide server IP ,PORT and an USERNAME.

Then just click the log in button and you are in if you get an welcome message from server.




Technology used:

i.   C++
ii.   Microsoft visual Studio 2010
iii.  MFC GUI
iv.  Winshock2 Socket
v.   TCP/UDP
vi.  Windows Thread


This project consist of  3 classes.

1. Client.cpp
2. ClientDlg.cpp
3. ClientCon.cpp


Lets start with core portion -- I mean the network level connection portion.

I have used the Winshock2 library for the purpose of network communication.

You can use this library on windows  with MVS 2010 by including library --


# include <winsock2.h>

And as it is multi user  and have GUI so we need thread. For this I have used Windows thread.
You can use this windows thread just by adding ---

# include <Windows.h>

So,  why we are late. Lets start....

ClientCon.cpp -- class works for network level connection and data send receive.

void ClientCon::StartConnect(string sAddress, int iPort, string sUsername)
{
    struct sockaddr_in server;
    char *message , server_reply[2000];
    int recv_size;
    m_pUser = sUsername;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return;
    }
    
    printf("Initialised.\n");
    
    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");
    
    
    server.sin_addr.s_addr = inet_addr(sAddress.c_str());
    server.sin_family = AF_INET;
    server.sin_port = htons( iPort );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return;
    }
    
    puts("Connected");
    
    //Receive a reply from the server
    while((recv_size = recv(s , server_reply , 2000 , 0)) != SOCKET_ERROR)
    {
        puts("Reply received\n");

        //Add a NULL terminating character to make it a proper string before printing
        server_reply[recv_size] = '\0';
        puts(server_reply);

        string sTempMsg ="\n"+string(server_reply)+"\n";
        m_pClient->ShowServerInfo(sTempMsg);
    }
   
}
 On StartConnect() Client initiate its socket and connect with server. Client also start listening for messages from server here. All the message from other client will traverse through server and will reach to every client.

This Class also have the function to send the message to server/other client.


void ClientCon::SendData(string sMessage)
{
    string sTemp = m_pUser +">>"+ sMessage+"\n";

    if( send(s , sTemp.c_str(), sTemp.size() , 0) < 0)
    {
        puts("Send failed");
        return;
    }
}
 This function is used to send the messages to the server by concatenating the user name to the messages.



COMPLETE CODE

NEXT

2 comments:

How to Generate and use the ssh key on Gerrit, github.io, gitlab, and bitbucket.

 Details can be found here -