CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    blocking sockets "connect" always returns 0

    Running on windows 7,
    using example code...
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    my "connect" function always immediately returns 0; even when not really connected to anything.
    And then my recv function will sit for about 30 seconds then timeout.
    I tried specifying every WinSoc ver on WSAStartup from 1.0 to 2.2, all have same effect.

    I'm using MinGW, No MFC. Is there something else I should be doing?

    Code:
        //request to startup use for sockets version 2.0
        WSADATA wsaData;
        int ret = WSAStartup(WSA_VER,&wsaData);
        if(ret!=0)
        {
            TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
            return FALSE;
        }
    
        //create a TCP socket
        SOCKET soc = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
        if(soc == INVALID_SOCKET)
        {
           TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
           WSACleanup();
           return FALSE;
        }
    
        //check if we have a host name or IP address
        BOOL bIsHost = isalpha(sServ[0]);
    
        //resolve IP if needed
        LPHOSTENT hostEntry = {0};
        if(bIsHost)//resolve hostname to IP address list
        {
            hostEntry = gethostbyname(sServ);
            //display IP address in debugger screen (just display the first IP in the list of returned IP's)
            TRACE("\nRESOLVED IP: %s",  (char*)inet_ntoa(*(in_addr*)hostEntry->h_addr_list[0])  );
            TRACE("\nRESOLVED HOSTNAME: %s",  (char*)hostEntry->h_name  );
            if(!hostEntry)
            {
               TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
               WSACleanup();
               return FALSE;
            }
        }
    
        //connection data
        sockaddr_in con;
        con.sin_family = AF_INET;
        if(bIsHost)
        { con.sin_addr.s_addr = *(u_long*)hostEntry->h_addr_list[0]; }//use the IP address we resolved
        else
        { con.sin_addr.s_addr = inet_addr(sServ); }//an IP was passed in, so just use that
        con.sin_port = htons(iPort);
    
        //try to connect
        ret = connect(soc,(SOCKADDR*)&con,sizeof(con));
        if(ret != 0)
        {
           TRACE("\nWSA ERROR: %s",ErrorToStr(WSAGetLastError()).data());
           WSACleanup();
           return FALSE;
        }
        TRACE("\n Connect Returned: %d, socket value = %d",ret,soc);

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: blocking sockets "connect" always returns 0

    What is the value of bIsHost? Are you trying to set yourself up as a server (perhaps bIsHost is true) and then connect to yourself? That obviously cannot work. You would need two instances of your program, one which sets itself up as a server and calls accept (and does not call connect), and the other which sets itself up as a client and calls connect (and does not call accept).

    Note that the code is not identical to the code at the MS site. If the code is unaltered, does it work as expected?

  3. #3
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    Re: blocking sockets "connect" always returns 0

    Unaltered MS code does the same thing.
    I'm not setting myself up as a server, I am connecting to my appache server on another computer when I want to test a good connection, and just making up fake local IP's when I want to test a failed connection.

    bIsHost changes depending on if sServ is "www.mywebsite.com" or "192.168.2.100"
    bIsHost is not the problem, the code all works, I can download files without problem. everything is working, the problem is "connect" always immediately returns 0 (returns instantly! it does not even block!), even if the ip address is 0.0.0.0 or 192.168.10.0 (which does not exist on my network), or if my network adapter is unplugged. It returns 0 as if it just made a successful connection. then when the code reaches my "recv" blocking function, the "recv" times out (it times out because im not really connected, even though the "connect" function lied and said I was)
    Problem is, the code should never go past the "connect" function when I specify and invalid IP, or IP that does not exist. but it does!.

    See attached picture for example of what seems impossible according to MSDN details of how "connect" works
    Attached Images Attached Images  
    Last edited by 12oclocker; November 1st, 2011 at 07:26 PM.

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: blocking sockets "connect" always returns 0

    I took these two examples and built them using VC2008. (To have just one app to run I did some minor changes. I had one function for each snippet and the client hardcoded to localhost but that shouldn't have any impact on the outcome)
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    If the server isn't running when the client is started the client terminates with 'Unable to connect to server!'
    If the server is started when the client is started the send/echo everything works alright.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    Re: blocking sockets "connect" always returns 0

    Thanks SMA, I created a new empty project, and copied and pasted those examples in, and re-linked to the libraries, then "connect" behaved as expect. So I'll need to do some digging to see why it's not working on my GUI app correctly. I'm unaware of anything that would cause connect to misbehave like this, but something is causing it, thanks!

  6. #6
    Join Date
    Jan 2008
    Location
    Earth
    Posts
    60

    Re: blocking sockets "connect" always returns 0

    edit...
    Last edited by 12oclocker; November 2nd, 2011 at 10:05 PM.

  7. #7
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: blocking sockets "connect" always returns 0

    What is WSA_VER?

    Is your code inside a message handler? If so, then perhaps you have re-entrancy issues.

    Perhaps you could post more of the code. It was not clear, for example, that your code was used in a GUI application. In fact, use of blocking socket calls in a GUI application, or any functions that block indefinitely in a GUI applications, is somewhat unexpected and perhaps ill-advised.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured