user2025098
user2025098

Reputation: 61

WSAConnect returns WSAEINVAL on WindowsXP

I use sockets in non-blocking mode, and sometimes WSAConnect function returns WSAEINVAL error. I investigate a problem and found, that it occurs if there is no pause (or it is very small ) between WSAConnect function calls. Does anyone know how to avoid this situation? Below you can found source code, that reproduce the problem. If I increase value of parameter in Sleep function to 50 or great - problem dissapear.
P.S. This problem reproduces only on Windows XP, on Win7 it works well.

    #undef UNICODE
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdio.h>
    #include <iostream>
    #include <windows.h>

    #pragma comment(lib, "Ws2_32.lib")

    static int getError(SOCKET sock)
    {
        DWORD error = WSAGetLastError();
        return error;
    }

    void main()
    {
        SOCKET sock;
        WSADATA wsaData;
        if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
            fprintf(stderr, "Socket Initialization Error. Program aborted\n");
            return;
        }

        for (int i = 0; i < 1000; ++i) {
            struct addrinfo hints;
            struct addrinfo *res = NULL;
            memset(&hints, 0, sizeof(hints));
            hints.ai_flags = AI_PASSIVE;
            hints.ai_socktype = SOCK_STREAM;
            hints.ai_family = AF_INET;
            hints.ai_protocol = IPPROTO_TCP;

            if (0 != getaddrinfo("172.20.1.59", "8091", &hints, &res)) {
                fprintf(stderr, "GetAddrInfo Error. Program aborted\n");
                closesocket(sock);
                WSACleanup();
                return;
            }

            struct addrinfo *ptr = 0;
            for (ptr=res; ptr != NULL ;ptr=ptr->ai_next) {
                sock = WSASocket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol, NULL, 0, NULL);    // 

                if (sock == INVALID_SOCKET) 
                    int err = getError(sock);
                else {
                    u_long noblock = 1;
                    if (ioctlsocket(sock, FIONBIO, &noblock) == SOCKET_ERROR) {
                        int err = getError(sock);
                        closesocket(sock);
                        sock = INVALID_SOCKET;
                    }   
                    break;
                }
            }
            int ret;

            do {
                ret = WSAConnect(sock, ptr->ai_addr, (int)ptr->ai_addrlen, NULL, NULL, NULL, NULL);

                if (ret == SOCKET_ERROR) {
                    int error = getError(sock);

                    if (error == WSAEWOULDBLOCK) {
                        Sleep(5);
                        continue;
                    }
                    else if (error == WSAEISCONN) {
                        fprintf(stderr, "+");
                        closesocket(sock);
                        sock = SOCKET_ERROR;
                        break;
                    }
                    else if (error == 10037) {
                        fprintf(stderr, "-");
                        closesocket(sock);
                        sock = SOCKET_ERROR;
                        break;
                    }
                    else {
                        fprintf(stderr, "Connect Error. [%d]\n", error);
                        closesocket(sock);
                        sock = SOCKET_ERROR;
                        break;
                    }
                }
                else {
                    int one = 1;
                    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char*)&one, sizeof(one));
                    fprintf(stderr, "OK\n");
                    break;
                }
            }
            while (1);
        }

        std::cout<<"end";
        char ch;
        std::cin >> ch;
    }

Upvotes: 2

Views: 1766

Answers (1)

Warren Young
Warren Young

Reputation: 42343

You've got a whole basketful of errors and questionable design and coding decisions here. I'm going to have to break them up into two groups:

Outright Errors

I expect if you fix all of the items in this section, your symptom will disappear, but I wouldn't want to speculate about which one is the critical fix:

  • Calling connect() in a loop on a single socket is simply wrong.

    If you mean to establish a connection, drop it, and reestablish it 1000 times, you need to call closesocket() at the end of each loop, then call socket() again to get a fresh socket. You can't keep re-connecting the same socket. Think of it like a power plug: if you want to plug it in twice, you have to unplug (closesocket()) between times.

    If instead you mean to establish 1000 simultaneous connections, you need to allocate a new socket with socket() on each iteration, connect() it, then go back around again to get another socket. It's basically the same loop as for the previous case, except without the closesocket() call.

    Beware that since XP is a client version of Windows, it's not optimized for handling thousands of simultaneous sockets.

  • Calling connect() again is not the correct response to WSAEWOULDBLOCK:

    if (error == WSAEWOULDBLOCK) {
        Sleep(5);
        continue; /// WRONG!
    }
    

    That continue code effectively commits the same error as above, but worse, if you only fix the previous error and leave this, this usage will then make your code start leaking sockets.

    WSAEWOULDBLOCK is not an error. All it means after a connect() on a nonblcoking socket is that the connection didn't get established immediately. The stack will notify your program when it does.

    You get that notification by calling one of select(), WSAEventSelect(), or WSAAsyncSelect(). If you use select(), the socket will be marked writable when the connection gets established. With the other two, you will get an FD_CONNECT event when the connection gets established.

    Which of these three APIs to call depends on why you want nonblocking sockets in the first place, and what the rest of the program will look like. What I see so far doesn't need nonblocking sockets at all, but I suppose you have some future plan that will inform your decision. I've written an article, Which I/O Strategy Should I Use (part of the Winsock Programmers' FAQ) which will help you decide which of these options to use; it may instead guide you to another option entirely.

  • You shouldn't use AI_PASSIVE and connect() on the same socket. Your use of AI_PASSIVE with getaddrinfo() tells the stack you intend to use this socket to accept incoming connections. Then you go and use that socket to make an outgoing connection.

    You've basically lied to the stack here. Computers find ways to get revenge when you lie to them.

  • Sleep() is never the right way to fix problems with Winsock. There are built-in delays within the stack that your program can see, such as TIME_WAIT and the Nagle algorithm, but Sleep() is not the right way to cope with these, either.

Questionable Coding/Design Decisions

This section is for things I don't expect to make your symptom go away, but you should consider fixing them anyway:

  • The main reason to use getaddrinfo() — as opposed to older, simpler functions like inet_addr() — is if you have to support IPv6. That kind of conflicts with your wish to support XP, since XP's IPv6 stack wasn't nearly as heavily tested during the time XP was the current version of Windows as its IPv4 stack. I would expect XP's IPv6 stack to still have bugs as a result, even if you've got all the patches installed.

    If you don't really need IPv6 support, doing it the old way might make your symptoms disappear. You might end up needing an IPv4-only build for XP.

  • This code:

    for (int i = 0; i < 1000; ++i) {
        // ...
        if (0 != getaddrinfo("172.20.1.59", "8091", &hints, &res)) {
    

    ...is inefficient. There is no reason you need to keep reinitializing res on each loop.

    Even if there is some reason I'm not seeing, you're leaking memory by not calling freeaddrinfo() on res.

    You should initialize this data structure once before you enter the loop, then reuse it on each iteration.

  • else if (error == 10037) {

    Why aren't you using WSAEALREADY here?

  • You don't need to use WSAConnect() here. You're using the 3-argument subset that Winsock shares with BSD sockets. You might as well use connect() here instead.

    There's no sense making your code any more complex than it has to be.

  • Why aren't you using a switch statement for this?

    if (error == WSAEWOULDBLOCK) {
        // ...
    }
    else if (error == WSAEISCONN) {
        // ...
    }
    // etc.
    
  • You shouldn't disable the Nagle algorithm:

    setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, ...);
    

Upvotes: 4

Related Questions