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

PREVIOUS


ClientDlg.cpp is the class where all the action from user's are taken and initiated. The interaction with GUI to logic is so great and easy which will help any developer to understand the flow of code.

Function for login --

void CClientDlg::OnBnClickedButton2()
{
    // TODO: Add your control notification handler code here
    cTh = AfxBeginThread(
    StaticThreadFunc,
    this);
    
   m_Thread_handle = cTh->m_hThread;
}
Start the thread for a login action.

UINT __cdecl CClientDlg::StaticThreadFunc(LPVOID pParam)
{
    CClientDlg *pYourClass = reinterpret_cast<CClientDlg*>(pParam);
    UINT retCode = pYourClass->ThreadFunc();

    return retCode;
}

UINT CClientDlg::ThreadFunc()
{
    // Do your thing, this thread now has access to all the classes member variables
   

    CString txtname;
    GetDlgItemText(IDC_EDIT2, txtname);

    CString portname;
    GetDlgItemText(IDC_EDIT3, portname);
    int iPort = _wtoi( portname.GetString() );
   
    CString username;
    GetDlgItemText(IDC_EDIT6, username);

    m_pClient = new ClientCon(this);

    CT2CA CStringToAscii(txtname);

      // construct a std::string using the LPCSTR input
     std::string sResultedString (CStringToAscii);
    
    CT2CA CStringToAscii2(username);

    std::string sResultedString2 (CStringToAscii2);

    m_pClient->StartConnect(sResultedString, iPort, sResultedString2);
    return 0;
}
 Used to take input from text box and send it to ClientCon class and start the Connection with server.

void CClientDlg::OnBnClickedButton4()
{
    // TODO: Add your control notification handler code here
    CString sTextData;
    GetDlgItemText(IDC_EDIT7, sTextData);
   
    CT2CA CStringToAscii(sTextData);

      // construct a std::string using the LPCSTR input
     std::string sResultedString (CStringToAscii);
     if(m_pClient != NULL)
     m_pClient->SendData(sResultedString);
     CWnd* pWnd = GetDlgItem(IDC_EDIT7);
     pWnd->SetWindowText(_T(""));
}

This function used to send the data to server for relay the messages to other client.


Complete Source Code SERVER and CLIENT:

DOWNLOAD

Post a Comment