I am new to MFC's CWinThread
and CAsyncSocket
, and trying to learn them myself with dialog based application.
Here is what I want to do: make a server/multi-client model: when the number clients connect to server, the server will make threads according to the number of clients and passes the socket connecting to the thread. I have refereed this article to do the passing: https://support.microsoft.com/en-us/kb/175668 .
I have successfully make thread on every connection, but...
My question is: Can I from the main windows(GUI) re-access all sockets that have been passed to threads to send(broadcast) data to all the clients?
This is how I do the passing:
On server side:
void CMyServerDlg::OnAccept(){
CConnectSoc temp_soc;
m_Listener.Accept(temp_soc);
CSocketThread *pThr = (CSocketThread*)AfxBeginThread(
RUNTIME_CLASS(CSocketThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
pThr->threadHandleSocket = temp_soc.Detach();
pThr->ResumeThread();
}
Note: m_Listener
is the object of the class that derived from CAsyncSocket
and CSocketThread
is derived from CWinThread
.
Inside the thread header, I added 2 lines:
Public:
CConnectSoc threadSocket;
SOCKET threadHandleSocket;
Inside the thread class .cpp:
BOOL CSocketThread::InitInstance(){
threadSocket.Attach(threadHandleSocket);
return TRUE;
}
Can someone tell me what to do next to send data to those socket?