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. ConnectEx fails

ConnectEx fails

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 Posts 4 Posters 3 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.
  • F followait

    fail

    //prepare function
    {
    DWORD dwBytes;
    int res = WSAIoctl(s,
    SIO_GET_EXTENSION_FUNCTION_POINTER,
    &GuidConnectEx,
    sizeof(GuidConnectEx),
    &g_lpfnConnectEx,
    sizeof(g_lpfnConnectEx),
    &dwBytes,
    NULL,
    NULL);

    	int err = WSAGetLastError();
    
    	assert(res==0);
    }	
    
    DWORD bytes\_sent;
    BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL);
    int err = WSAGetLastError(); // err==87 (indicates invalid parameter)
    assert(ok); // failed here!
    

    succeed

    int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
    assert(ok==0);
    

    why?

    Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #2

    The documentation says that when you call ConnectEx, the socket must be already bound. The connect function binds the socket for you. That must be why connect succeeds when ConnectEx fails.

    F 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      The documentation says that when you call ConnectEx, the socket must be already bound. The connect function binds the socket for you. That must be why connect succeeds when ConnectEx fails.

      F Offline
      F Offline
      followait
      wrote on last edited by
      #3

      tried, still not work

      int main()
      {
      WSADATA wsa;
      int res = WSAStartup(MAKEWORD(2,2), &wsa);
      assert(res==0);

      SOCKADDR\_IN addr\_in;
      addr\_in.sin\_family = AF\_INET;
      addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
      addr\_in.sin\_port = htons(5150);
      ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero));
      
      SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0);
      assert(s!=INVALID\_SOCKET);
      SOCKADDR\_IN addr\_in\_local;
      addr\_in\_local.sin\_family = AF\_INET;
      addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY;
      addr\_in\_local.sin\_port = 0;
      ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero));
      res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local));
      assert(res==0);
      
      //prepare function
      {
      	DWORD dwBytes;
      	int res = WSAIoctl(s,
      		SIO\_GET\_EXTENSION\_FUNCTION\_POINTER,
      		&GuidConnectEx,
      		sizeof(GuidConnectEx),
      		&g\_lpfnConnectEx,
      		sizeof(g\_lpfnConnectEx),
      		&dwBytes,
      		NULL,
      		NULL);
      
      	int err = WSAGetLastError();
      
      	assert(res==0);
      }	
      
      DWORD bytes\_sent;
      BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL);
      int err = WSAGetLastError();
      assert(ok);
      //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
      //assert(ok==0);
      
      for (int i=0; i<10; ++i)
      {
      	char buf\[256\];
      	sprintf\_s(buf, sizeof(buf), "message %d", i);
      	WSABUF wsa\_buf;
      	wsa\_buf.buf = buf;
      	wsa\_buf.len = strlen(buf);
      	DWORD bytes\_sent;
      	int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL);
      	assert(res==0 && bytes\_sent==wsa\_buf.len);
      	printf\_s("sent: %s\\n", buf);
      	Sleep(1000);
      }
      
      WSACleanup();
      
      return 0;
      

      }

      L L 2 Replies Last reply
      0
      • F followait

        tried, still not work

        int main()
        {
        WSADATA wsa;
        int res = WSAStartup(MAKEWORD(2,2), &wsa);
        assert(res==0);

        SOCKADDR\_IN addr\_in;
        addr\_in.sin\_family = AF\_INET;
        addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
        addr\_in.sin\_port = htons(5150);
        ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero));
        
        SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0);
        assert(s!=INVALID\_SOCKET);
        SOCKADDR\_IN addr\_in\_local;
        addr\_in\_local.sin\_family = AF\_INET;
        addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY;
        addr\_in\_local.sin\_port = 0;
        ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero));
        res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local));
        assert(res==0);
        
        //prepare function
        {
        	DWORD dwBytes;
        	int res = WSAIoctl(s,
        		SIO\_GET\_EXTENSION\_FUNCTION\_POINTER,
        		&GuidConnectEx,
        		sizeof(GuidConnectEx),
        		&g\_lpfnConnectEx,
        		sizeof(g\_lpfnConnectEx),
        		&dwBytes,
        		NULL,
        		NULL);
        
        	int err = WSAGetLastError();
        
        	assert(res==0);
        }	
        
        DWORD bytes\_sent;
        BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL);
        int err = WSAGetLastError();
        assert(ok);
        //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
        //assert(ok==0);
        
        for (int i=0; i<10; ++i)
        {
        	char buf\[256\];
        	sprintf\_s(buf, sizeof(buf), "message %d", i);
        	WSABUF wsa\_buf;
        	wsa\_buf.buf = buf;
        	wsa\_buf.len = strlen(buf);
        	DWORD bytes\_sent;
        	int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL);
        	assert(res==0 && bytes\_sent==wsa\_buf.len);
        	printf\_s("sent: %s\\n", buf);
        	Sleep(1000);
        }
        
        WSACleanup();
        
        return 0;
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        followait wrote:

        tried, still not work

        What does this mean? Please explain which part of the program does not work, what results you receive, what messages you see, what error codes are returned. We cannot guess what happens by looking at your source code.

        It's time for a new signature.

        F 1 Reply Last reply
        0
        • L Lost User

          followait wrote:

          tried, still not work

          What does this mean? Please explain which part of the program does not work, what results you receive, what messages you see, what error codes are returned. We cannot guess what happens by looking at your source code.

          It's time for a new signature.

          F Offline
          F Offline
          followait
          wrote on last edited by
          #5

          As he suggests, bind the socket used by ConnectEx, but still can't connect successfully.

          L 1 Reply Last reply
          0
          • F followait

            As he suggests, bind the socket used by ConnectEx, but still can't connect successfully.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            followait wrote:

            but still can't connect successfully.

            Did you actually read my previous post? What is the error code returned when your connect call fails?

            It's time for a new signature.

            F 1 Reply Last reply
            0
            • L Lost User

              followait wrote:

              but still can't connect successfully.

              Did you actually read my previous post? What is the error code returned when your connect call fails?

              It's time for a new signature.

              F Offline
              F Offline
              followait
              wrote on last edited by
              #7

              sure, the error code of ConnectEx is 87, it means "The parameter is incorrect.".

              L 3 Replies Last reply
              0
              • F followait

                sure, the error code of ConnectEx is 87, it means "The parameter is incorrect.".

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                followait wrote:

                the error code of ConnectEx is 87, it means "The parameter is incorrect.".

                OK, so check all your parameters and make sure that they contain values appropriate to the call.

                It's time for a new signature.

                1 Reply Last reply
                0
                • F followait

                  sure, the error code of ConnectEx is 87, it means "The parameter is incorrect.".

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  I just took a copy of your code and ran it on my Windows 7 system and it runs fine; must be something local to you. [edit]Actually no it didn't, I had to change a couple of things and misread a fail status. I will test some more tomorrow in the absence of a solution.[/edit]

                  It's time for a new signature.

                  modified on Saturday, August 7, 2010 1:05 PM

                  1 Reply Last reply
                  0
                  • F followait

                    tried, still not work

                    int main()
                    {
                    WSADATA wsa;
                    int res = WSAStartup(MAKEWORD(2,2), &wsa);
                    assert(res==0);

                    SOCKADDR\_IN addr\_in;
                    addr\_in.sin\_family = AF\_INET;
                    addr\_in.sin\_addr.s\_addr = inet\_addr("127.0.0.1");
                    addr\_in.sin\_port = htons(5150);
                    ZeroMemory(&addr\_in.sin\_zero, sizeof(addr\_in.sin\_zero));
                    
                    SOCKET s = WSASocket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP, NULL, 0, 0);
                    assert(s!=INVALID\_SOCKET);
                    SOCKADDR\_IN addr\_in\_local;
                    addr\_in\_local.sin\_family = AF\_INET;
                    addr\_in\_local.sin\_addr.s\_addr = INADDR\_ANY;
                    addr\_in\_local.sin\_port = 0;
                    ZeroMemory(&addr\_in\_local.sin\_zero, sizeof(addr\_in\_local.sin\_zero));
                    res = bind(s, (sockaddr\*)&addr\_in\_local, sizeof(addr\_in\_local));
                    assert(res==0);
                    
                    //prepare function
                    {
                    	DWORD dwBytes;
                    	int res = WSAIoctl(s,
                    		SIO\_GET\_EXTENSION\_FUNCTION\_POINTER,
                    		&GuidConnectEx,
                    		sizeof(GuidConnectEx),
                    		&g\_lpfnConnectEx,
                    		sizeof(g\_lpfnConnectEx),
                    		&dwBytes,
                    		NULL,
                    		NULL);
                    
                    	int err = WSAGetLastError();
                    
                    	assert(res==0);
                    }	
                    
                    DWORD bytes\_sent;
                    BOOL ok = (\*g\_lpfnConnectEx)(s, (sockaddr\*)&addr\_in, sizeof(addr\_in), NULL, 0, &bytes\_sent, NULL);
                    int err = WSAGetLastError();
                    assert(ok);
                    //int ok = connect(s, (sockaddr\*)&addr\_in, sizeof(addr\_in));
                    //assert(ok==0);
                    
                    for (int i=0; i<10; ++i)
                    {
                    	char buf\[256\];
                    	sprintf\_s(buf, sizeof(buf), "message %d", i);
                    	WSABUF wsa\_buf;
                    	wsa\_buf.buf = buf;
                    	wsa\_buf.len = strlen(buf);
                    	DWORD bytes\_sent;
                    	int res = WSASend(s, &wsa\_buf, 1, &bytes\_sent, 0, NULL, NULL);
                    	assert(res==0 && bytes\_sent==wsa\_buf.len);
                    	printf\_s("sent: %s\\n", buf);
                    	Sleep(1000);
                    }
                    
                    WSACleanup();
                    
                    return 0;
                    

                    }

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #10

                    followait wrote:

                    BOOL ok = (*g_lpfnConnectEx)(s, (sockaddr*)&addr_in, sizeof(addr_in), NULL, 0, &bytes_sent, NULL);

                    Now what would be the most fishy aspect of that statement? You should realize that for every cast the programmer takes the responsibility away from the compiler; you are simply passing a SOCKADDR_IN thingy and casting it to something else (a sockaddr) in order to make the compiler happy. However if they aren't compatible, that will not work out at run-time. So back to the documentation and the drawing board. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    1 Reply Last reply
                    0
                    • F followait

                      sure, the error code of ConnectEx is 87, it means "The parameter is incorrect.".

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #11

                      Well, I went and did what I should have done in the first place and looked here[^] at the MSDN documentation. Take a look at the final parameter lpOverlapped which must not be NULL, and compare with your code.

                      It's time for a new signature.

                      F 1 Reply Last reply
                      0
                      • L Lost User

                        Well, I went and did what I should have done in the first place and looked here[^] at the MSDN documentation. Take a look at the final parameter lpOverlapped which must not be NULL, and compare with your code.

                        It's time for a new signature.

                        F Offline
                        F Offline
                        followait
                        wrote on last edited by
                        #12

                        Fine, it is the reason why it doesn't work. So ConnectEx works only in asynchronous mode, doesn't it? Thanks a lot.

                        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