Threaded multi chat room server source code in c++ with MFC Part - 3

PREVIOUS 

Now we will talk about the other MFC GUI portion which I have created and used threaded model.

When in GUI the start button is clicked by providing port number on PORT text box this server start by creating a thread independent from GUI. You can start the server by ENTER also.


Here is some code snap for help --


void CServerDlg::OnBnClickedButton1()
{
    // TODO: Add your control notification handler code here

    cTh = AfxBeginThread(
    StaticThreadFunc,
    this);
   
    //cTh->m_bAutoDelete = FALSE;
    m_Thread_handle = cTh->m_hThread;
   
}
Here we create a windows thread for back end action.

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

    return retCode;
}

UINT CServerDlg::ThreadFunc()
{
    // Do your thing, this thread now has access to all the classes member variables
    CString txtname;
    GetDlgItemText(IDC_EDIT1, txtname);
    int iPort = _wtoi( txtname.GetString() );

    if(iPort == 0)
    {
        return -1;
    }
    m_pServer = new ServerManager(this);
    m_pServer->StartListening(iPort);
    return 0;
}
On ThredFunc() we take the input from text box and pass this value on to ServerMnager class and start the server.


Post a Comment