CSocket problem
-
Hi, I have an APPWizard project which has CMainFrm class I added a CWinThread and CSocket derived classes to the project.(CRThread and CRSocket my classes) when I connect to a server with CRSocket whithin CMainFrm class the OnReceive event of the socket trigers (no problem). But when I transfer the same code to the thread CRThread the OnRecive event of the CRSocket does not triger (connection is OK). I think that there is a problem with messages received by the CWinThread derived class (CRThread) Can you yelp me with this problem, Thank you in advance, Seval YILMAZ sample code m_pRSocket = new CSocket(); m_pRSocket->Create(); m_pRSocket->Connect(_T("10.10.1.1"), 1550);
-
Hi, I have an APPWizard project which has CMainFrm class I added a CWinThread and CSocket derived classes to the project.(CRThread and CRSocket my classes) when I connect to a server with CRSocket whithin CMainFrm class the OnReceive event of the socket trigers (no problem). But when I transfer the same code to the thread CRThread the OnRecive event of the CRSocket does not triger (connection is OK). I think that there is a problem with messages received by the CWinThread derived class (CRThread) Can you yelp me with this problem, Thank you in advance, Seval YILMAZ sample code m_pRSocket = new CSocket(); m_pRSocket->Create(); m_pRSocket->Connect(_T("10.10.1.1"), 1550);
You should know, that the CSocket member is primarily used for absolute, defined network traffic, which happens in a synchronous, sequential order already outlined. If you use asynchronous communication (a response from the server or a request from a client may arrive at any given time), you should derive your class from CAsyncSocket. This is the base class of CSocket and offers much greater flexibility, removing most of the automatic routines. Know, however, that asynchronous communication requires much more thought and design to be implemented properly. As for the problem itself, make sure that the supporting thread is not terminated prematurely, and that you have specified the option for the client socket that you wish to handle received data notifications (See CAsyncSocket::OnReceive for details). If these don't help, then the problem lies somewhere deeper. The socket object, by itself, is independent of where it is operated. That is, when the object is initialized properly, the framework (your computer) will notify the object that data is available or that data can be sent. The socket, as far as I can say based on the MSDN reference, is independent of the thread on which it is running. But the main problem might be in the usage of CSocket instead of CAsyncSocket. Try implementing your program by using CAsyncSocket instead, and see how it works. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.
-
Hi, I have an APPWizard project which has CMainFrm class I added a CWinThread and CSocket derived classes to the project.(CRThread and CRSocket my classes) when I connect to a server with CRSocket whithin CMainFrm class the OnReceive event of the socket trigers (no problem). But when I transfer the same code to the thread CRThread the OnRecive event of the CRSocket does not triger (connection is OK). I think that there is a problem with messages received by the CWinThread derived class (CRThread) Can you yelp me with this problem, Thank you in advance, Seval YILMAZ sample code m_pRSocket = new CSocket(); m_pRSocket->Create(); m_pRSocket->Connect(_T("10.10.1.1"), 1550);
Q140527 might be useful here, especially the "more information" section, which says: To Accept a socket in the context of one thread and then begin using it in the context of another thread, you need to be sure to detach the CAsyncSocket object in the first thread and attach a different CAsyncSocket object in the second thread. The KB article also has some sample code. Graham
-
Hi, I have an APPWizard project which has CMainFrm class I added a CWinThread and CSocket derived classes to the project.(CRThread and CRSocket my classes) when I connect to a server with CRSocket whithin CMainFrm class the OnReceive event of the socket trigers (no problem). But when I transfer the same code to the thread CRThread the OnRecive event of the CRSocket does not triger (connection is OK). I think that there is a problem with messages received by the CWinThread derived class (CRThread) Can you yelp me with this problem, Thank you in advance, Seval YILMAZ sample code m_pRSocket = new CSocket(); m_pRSocket->Create(); m_pRSocket->Connect(_T("10.10.1.1"), 1550);
1. Did you call AfxSocketInit in your thread? You probably did since the socket connection is OK. 2. Does your thread have a message pump? The OnReceive event depends on windows messages. If your thread does not have a message pump, then the event will never be fired. The simplest way to add a message pump is create a modal dialog (hide it if you wish) from the thread after the socket connection.
::MessageBox(NULL, _T("I am a message pump for the socket"), NULL, MB_OK);
Good luck.[My articles and software tools