Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in attatching SOCKET handle to CSocket object

problem in attatching SOCKET handle to CSocket object

Scheduled Pinned Locked Moved C / C++ / MFC
helpcomsysadmin
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ramana g
    wrote on last edited by
    #1

    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.

    V M 2 Replies Last reply
    0
    • R ramana g

      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.

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      I would suggest you to check if the SOCKET handle, obtained by Accept and then passed to your thread, is valid, i.e. is not "-1". Then put a breakpoint at Attach 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.

      1 Reply Last reply
      0
      • R ramana g

        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.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        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?

        R 1 Reply Last reply
        0
        • M Mark Salsbery

          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?

          R Offline
          R Offline
          ramana g
          wrote on last edited by
          #4

          its a GUI based application

          M 1 Reply Last reply
          0
          • R ramana g

            its a GUI based application

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • M Mark Salsbery

              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

              R Offline
              R Offline
              ramana g
              wrote on last edited by
              #6

              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.

              M 1 Reply Last reply
              0
              • R ramana g

                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.

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #7

                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;
                }

                R 1 Reply Last reply
                0
                • M Mark Salsbery

                  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;
                  }

                  R Offline
                  R Offline
                  ramana g
                  wrote on last edited by
                  #8

                  ya. i have the code in InitInstance() method of APP class if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; }

                  M 1 Reply Last reply
                  0
                  • R ramana g

                    ya. i have the code in InitInstance() method of APP class if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; }

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #9

                    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 :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups