problem in attatching SOCKET handle to CSocket object
-
Hi All! i have some problem in attatching a SOCKET handle to a CSocket object. I'm creating a Socket on Server side, and accepting the conncetion using a temperory socket object, and retrieving the SOCKET handle by using CSocket::Detach(), below is the code i used... At Server: in the main thread. CListeningSocket ServerSocket;// class CListeningSocket : public CSocket CSocket TempSocket; ServerSocket.Create(port); ServerSocket.Listen(); ServerSocket.Accept(TempSocket); SOCKET hSocket = TempSocket.Detach(); and i'm passing this handle to a Thread. (trying to create a separate thread for each client) ( this techinque is mentioned in one of the articles from Microsoft the link is [url]http://support.microsoft.com/kb/175668\[/url\] ) In the CSocketThread: i'll receive the SOCKET handle as parameter (even tried taking the SOCKET handle as a member variable, using a member function to set the m_hSocket) CClientSocket *pSocketForClient = new CClientSocket; SOCKET hSocket = (SOCKET)Param; pSocketForClient->Attatch(hSocket); // Problem is here The problem is while trying to attatch its trowing some exception. I tried using ::GetLastError() to find the error code, but its returning ZERO. Even i tried to catch exception but, i'm unable to catch using catch(CException e). i modified the code as try { pSocketForClient->Attatch(hSocket); } catch(CException e) { // some code to find exception } but its not catching the exception. i'm able to catch the exception by catch(...) but as u know, it is of no use to find the error Client code: CSocket Socket; Socket.Create(); Socket.Connect(ip, port); PLAESE HELP ME ramana.
-
Hi All! i have some problem in attatching a SOCKET handle to a CSocket object. I'm creating a Socket on Server side, and accepting the conncetion using a temperory socket object, and retrieving the SOCKET handle by using CSocket::Detach(), below is the code i used... At Server: in the main thread. CListeningSocket ServerSocket;// class CListeningSocket : public CSocket CSocket TempSocket; ServerSocket.Create(port); ServerSocket.Listen(); ServerSocket.Accept(TempSocket); SOCKET hSocket = TempSocket.Detach(); and i'm passing this handle to a Thread. (trying to create a separate thread for each client) ( this techinque is mentioned in one of the articles from Microsoft the link is [url]http://support.microsoft.com/kb/175668\[/url\] ) In the CSocketThread: i'll receive the SOCKET handle as parameter (even tried taking the SOCKET handle as a member variable, using a member function to set the m_hSocket) CClientSocket *pSocketForClient = new CClientSocket; SOCKET hSocket = (SOCKET)Param; pSocketForClient->Attatch(hSocket); // Problem is here The problem is while trying to attatch its trowing some exception. I tried using ::GetLastError() to find the error code, but its returning ZERO. Even i tried to catch exception but, i'm unable to catch using catch(CException e). i modified the code as try { pSocketForClient->Attatch(hSocket); } catch(CException e) { // some code to find exception } but its not catching the exception. i'm able to catch the exception by catch(...) but as u know, it is of no use to find the error Client code: CSocket Socket; Socket.Create(); Socket.Connect(ip, port); PLAESE HELP ME ramana.
I would suggest you to check if the
SOCKET
handle, obtained byAccept
and then passed to your thread, is valid, i.e. is not "-1". Then put a breakpoint atAttach
line and then continue execution using Step Into (F11). You should see the MFC source code and found the cause of the exception. In order to catch the exception, try a different declaration:try { . . . } catch( const CException * e) { . . . } catch( const CException & e) { . . . }
I hope this helps.
-
Hi All! i have some problem in attatching a SOCKET handle to a CSocket object. I'm creating a Socket on Server side, and accepting the conncetion using a temperory socket object, and retrieving the SOCKET handle by using CSocket::Detach(), below is the code i used... At Server: in the main thread. CListeningSocket ServerSocket;// class CListeningSocket : public CSocket CSocket TempSocket; ServerSocket.Create(port); ServerSocket.Listen(); ServerSocket.Accept(TempSocket); SOCKET hSocket = TempSocket.Detach(); and i'm passing this handle to a Thread. (trying to create a separate thread for each client) ( this techinque is mentioned in one of the articles from Microsoft the link is [url]http://support.microsoft.com/kb/175668\[/url\] ) In the CSocketThread: i'll receive the SOCKET handle as parameter (even tried taking the SOCKET handle as a member variable, using a member function to set the m_hSocket) CClientSocket *pSocketForClient = new CClientSocket; SOCKET hSocket = (SOCKET)Param; pSocketForClient->Attatch(hSocket); // Problem is here The problem is while trying to attatch its trowing some exception. I tried using ::GetLastError() to find the error code, but its returning ZERO. Even i tried to catch exception but, i'm unable to catch using catch(CException e). i modified the code as try { pSocketForClient->Attatch(hSocket); } catch(CException e) { // some code to find exception } but its not catching the exception. i'm able to catch the exception by catch(...) but as u know, it is of no use to find the error Client code: CSocket Socket; Socket.Create(); Socket.Connect(ip, port); PLAESE HELP ME ramana.
Is this in an MFC app (yes, I know you're using an MFC class CSocket)? Is it a console/service application or a GUI application?
-
Is this in an MFC app (yes, I know you're using an MFC class CSocket)? Is it a console/service application or a GUI application?
-
I would start by stepping into your CClientSocket::Attatch() function in the debugger and you'll see what line the exception occurs on. Put a breakpoint on this line pSocketForClient->Attatch(hSocket); // Problem is here and use F11 key to step into the Attatch() function. There' s probably a pointer issue of some kind. If you post the code for Attatch() maybe I can help a little :) Mark
-
I would start by stepping into your CClientSocket::Attatch() function in the debugger and you'll see what line the exception occurs on. Put a breakpoint on this line pSocketForClient->Attatch(hSocket); // Problem is here and use F11 key to step into the Attatch() function. There' s probably a pointer issue of some kind. If you post the code for Attatch() maybe I can help a little :) Mark
Hi Mark I dint override the Attatch() member function of CAsyncSocket class. I stepped in to the framework code of MFC. i'm giving the details, where it is going wrong. File : MAP_PP.CPP Function : void* CMapPtrToPtr::GetValueAt(void* key) const Line: 179 Code: if (m_pHashTable == NULL) return NULL; because of the abov code, some runtime error is occuring. Pls suggest me, where it may go wrong? ramana.
-
Hi Mark I dint override the Attatch() member function of CAsyncSocket class. I stepped in to the framework code of MFC. i'm giving the details, where it is going wrong. File : MAP_PP.CPP Function : void* CMapPtrToPtr::GetValueAt(void* key) const Line: 179 Code: if (m_pHashTable == NULL) return NULL; because of the abov code, some runtime error is occuring. Pls suggest me, where it may go wrong? ramana.
Do you have something like this in your app class' InitInstance() ?
// Initialize windows sockets
if (!AfxSocketInit())
{
::SetCursor(::LoadCursor(0, IDC_ARROW));
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
} -
Do you have something like this in your app class' InitInstance() ?
// Initialize windows sockets
if (!AfxSocketInit())
{
::SetCursor(::LoadCursor(0, IDC_ARROW));
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
} -
ya. i have the code in InitInstance() method of APP class if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; }
hmmm should work. The only thing I can think of at this point is that your thread that you pass the socket to needs to have a message loop because the CSocket/CAsyncSocket classes use a window for socket messages. I'm not sure if this is related to the exception you are getting though :confused: When you stepped into the Attach function, how far did it get? Specifically, how far into the CAsyncSocket::AttachHandle() did it go? Did the CreateEx() for the window succeed? We'll figure this out :)