Using MFC's socket within the Process' threads
-
Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit
-
Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit
You secondary thread may possibly post messages to the primary one and let the latter do socket processing. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit
...or you can stop using CSocket (it's really only a useful class for the most basic socket application) and use CAsyncSocket instead. If you'll be using the same socket from different threads, you'll want to use events instead of window messages for socket notifications. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit
AmitCohen222 wrote:
if I understand correctly it is since the only thread allowed to use this socket is the one created it
Not exactly, it's got to do with a bug in MFC. See knowledge base Q193101[^] - I had a similar problem and calling AfxSocketInit() in each thread fixed it.
-
AmitCohen222 wrote:
if I understand correctly it is since the only thread allowed to use this socket is the one created it
Not exactly, it's got to do with a bug in MFC. See knowledge base Q193101[^] - I had a similar problem and calling AfxSocketInit() in each thread fixed it.
That bug appears only when linking statically, and is probably not the cause of the problem. For a solution on using a CSocket object in a thread different from the thread that created it, see "How to pass a socket connection between threads in an MFC application in Visual C++" at http://support.microsoft.com/kb/175668[^]. Basically, you need surround the thread hand-off with calls to
CAsyncSocket::Detach()
andCAsyncSocket::Attach()
. Mike -
That bug appears only when linking statically, and is probably not the cause of the problem. For a solution on using a CSocket object in a thread different from the thread that created it, see "How to pass a socket connection between threads in an MFC application in Visual C++" at http://support.microsoft.com/kb/175668[^]. Basically, you need surround the thread hand-off with calls to
CAsyncSocket::Detach()
andCAsyncSocket::Attach()
. MikeOh yes, I missed that point. You do need to detach the original socket.