cilu
March 1st, 2004, 06:54 AM
I encount a problem with CAsyncSocket when I have multiple threads. I will try to explain my problem in a few lines.
class CServerSocket : public ACsyncSocket
{
...
Startup() { /* calls CAsyncSocket::Socket, CAsyncSocket::Bind, etc */ };
ShutDown() { /* calls for CAsyncSocket::Close() */ };
Send(...) { /* uses CAsyncSocket::Send */ };
OnReceive() { /* send the receive message to the GUI */ };
...
}
class CServer : public CWindThread
{
CServerSocket *socket;
...
Run() {/* reads messages posted to the thread and uses socket->Send() to deliver it*/};
StartUp() { /* calls for socket->StartUp */ };
ShutDown() { /* calls for socket->ShutDown */ };
}
class CMyDialog : public CDialog
{
CServer *server;
...
OnButtonShartup() { /* initializes server, and calls server->CreateThread() */ };
OnSendMessage() { /* calls server->PostThreadMessage() */ };
}
On client side I have a code very similar with this one; only CServerSocket is replaced with a CClientSocket and CServer with CClient.
class CClientSocket : public CAsyncSocket
{
...
Connect() { /* calls CAsyncSocket::Socket and CAsyncSocket::Connect */ };
Disconnect() { /* CAsyncSocket::Close */ };
Send(...) { /* CAsyncSocket::Send */ };
OnReceive() {};
}
The problem is that the code doesn't work. The messages are sent but never received. Howerver, if I do not create CSocket and CClient threads (which are almost identical), i.e. never call CreateThread, but use instead server->StartUp()/ShutDown() and client->Connect()/Disconnect() the code works just fine. Also if I don't use at all CServer and CClient classes and I use from CMyDialog (both in server and client applications) CServerSocket/CClientSocket directly, it works.
So my problem is that I do not understand why the messages fail to arrive at the receiver (OnReceive is never called), either the receiver is the client or the server, when I have another thread in my application.
Can anyone help me with this? Thank you.
class CServerSocket : public ACsyncSocket
{
...
Startup() { /* calls CAsyncSocket::Socket, CAsyncSocket::Bind, etc */ };
ShutDown() { /* calls for CAsyncSocket::Close() */ };
Send(...) { /* uses CAsyncSocket::Send */ };
OnReceive() { /* send the receive message to the GUI */ };
...
}
class CServer : public CWindThread
{
CServerSocket *socket;
...
Run() {/* reads messages posted to the thread and uses socket->Send() to deliver it*/};
StartUp() { /* calls for socket->StartUp */ };
ShutDown() { /* calls for socket->ShutDown */ };
}
class CMyDialog : public CDialog
{
CServer *server;
...
OnButtonShartup() { /* initializes server, and calls server->CreateThread() */ };
OnSendMessage() { /* calls server->PostThreadMessage() */ };
}
On client side I have a code very similar with this one; only CServerSocket is replaced with a CClientSocket and CServer with CClient.
class CClientSocket : public CAsyncSocket
{
...
Connect() { /* calls CAsyncSocket::Socket and CAsyncSocket::Connect */ };
Disconnect() { /* CAsyncSocket::Close */ };
Send(...) { /* CAsyncSocket::Send */ };
OnReceive() {};
}
The problem is that the code doesn't work. The messages are sent but never received. Howerver, if I do not create CSocket and CClient threads (which are almost identical), i.e. never call CreateThread, but use instead server->StartUp()/ShutDown() and client->Connect()/Disconnect() the code works just fine. Also if I don't use at all CServer and CClient classes and I use from CMyDialog (both in server and client applications) CServerSocket/CClientSocket directly, it works.
So my problem is that I do not understand why the messages fail to arrive at the receiver (OnReceive is never called), either the receiver is the client or the server, when I have another thread in my application.
Can anyone help me with this? Thank you.