CSocket threading issue
-
Hello, I have written a client server application using sockets. In this application, server will send data to client. I have used CSocket(MFC) for this purpose. Server side application is working fine but problems start as soon as i had intoduced threading inside it. Below is my code: /////////////////////////////////////////////////////////// UINT __cdecl ThreadProc( LPVOID pParam ); // Signature of thread proc AfxSocketInit(NULL); CSocket sockSrvr; sockSrvr.Create(PORT); BOOL bListen = sockSrvr.Listen(); while (1) { CSocket sockRecv; sockSrvr.Accept(sockRecv); AfxBeginThread(ThreadProc, &sockRecv); Sleep(5000); } /////////////////////////////////////////////////////////// I pass address of CSocket as parameter of 'ThreadProc' and as soon as control enters 'ThreadProc' iget a debug assertion faliure from sockcore.cpp at the following line: ASSERT(pState->m_hSocketWindow != NULL); Can anybody of you please help me i am not able to understan wats wrong to this code? Thanks in Advance!! Regards Tony
-
Hello, I have written a client server application using sockets. In this application, server will send data to client. I have used CSocket(MFC) for this purpose. Server side application is working fine but problems start as soon as i had intoduced threading inside it. Below is my code: /////////////////////////////////////////////////////////// UINT __cdecl ThreadProc( LPVOID pParam ); // Signature of thread proc AfxSocketInit(NULL); CSocket sockSrvr; sockSrvr.Create(PORT); BOOL bListen = sockSrvr.Listen(); while (1) { CSocket sockRecv; sockSrvr.Accept(sockRecv); AfxBeginThread(ThreadProc, &sockRecv); Sleep(5000); } /////////////////////////////////////////////////////////// I pass address of CSocket as parameter of 'ThreadProc' and as soon as control enters 'ThreadProc' iget a debug assertion faliure from sockcore.cpp at the following line: ASSERT(pState->m_hSocketWindow != NULL); Can anybody of you please help me i am not able to understan wats wrong to this code? Thanks in Advance!! Regards Tony
tony_udz wrote:
while (1) { CSocket sockRecv; sockSrvr.Accept(sockRecv); AfxBeginThread(ThreadProc, &sockRecv); Sleep(5000); }
I think problem is you are creating sockRecv on stack. As soon as you create the thread it will go out of scope and destructor will be called and connection will close. Try this
CSocket *sockRecv = new CSocket();
dont forget to delete the socket in the thread after the operations are complete. I hope it helps. I did not know that we can not use the worker thread with the CSocket See Roger's Reply :)
Regards, Sandip.
modified on Thursday, September 4, 2008 4:56 AM
-
Hello, I have written a client server application using sockets. In this application, server will send data to client. I have used CSocket(MFC) for this purpose. Server side application is working fine but problems start as soon as i had intoduced threading inside it. Below is my code: /////////////////////////////////////////////////////////// UINT __cdecl ThreadProc( LPVOID pParam ); // Signature of thread proc AfxSocketInit(NULL); CSocket sockSrvr; sockSrvr.Create(PORT); BOOL bListen = sockSrvr.Listen(); while (1) { CSocket sockRecv; sockSrvr.Accept(sockRecv); AfxBeginThread(ThreadProc, &sockRecv); Sleep(5000); } /////////////////////////////////////////////////////////// I pass address of CSocket as parameter of 'ThreadProc' and as soon as control enters 'ThreadProc' iget a debug assertion faliure from sockcore.cpp at the following line: ASSERT(pState->m_hSocketWindow != NULL); Can anybody of you please help me i am not able to understan wats wrong to this code? Thanks in Advance!! Regards Tony
You cannot use a worker thread for MFC sockets, you have to use a UI-thread with a message pump. Read more here[^]. When it comes to CSocket it's virtually useless when developing anything other than toy applications. Mostly because it is blocking when both reading and writing. Use CAsyncSocket instead and you probably won't even need a secondary thread.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
You cannot use a worker thread for MFC sockets, you have to use a UI-thread with a message pump. Read more here[^]. When it comes to CSocket it's virtually useless when developing anything other than toy applications. Mostly because it is blocking when both reading and writing. Use CAsyncSocket instead and you probably won't even need a secondary thread.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Roger Stoltz wrote:
You cannot use a worker thread for MFC sockets, you have to use a UI-thread with a message pump. Read more here[^].
Thanks for the update about CSocket I was not aware of this.
Regards, Sandip.
SandipG :) wrote:
Thanks for the update about CSocket I was not aware of this.
You're welcome Sandip. :)
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
You cannot use a worker thread for MFC sockets, you have to use a UI-thread with a message pump. Read more here[^]. When it comes to CSocket it's virtually useless when developing anything other than toy applications. Mostly because it is blocking when both reading and writing. Use CAsyncSocket instead and you probably won't even need a secondary thread.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown