WSAEWOULDBLOCK and WSAEISCONN

This is a discussion on WSAEWOULDBLOCK and WSAEISCONN within the Networking/Device Communication forums, part of the General Programming Boards category; Hello. I wrote this code: Code: SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in socket_address; //snip - initialize the socket_address' members ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Location
    Estonia
    Posts
    131

    WSAEWOULDBLOCK and WSAEISCONN

    Hello.

    I wrote this code:
    Code:
    SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    sockaddr_in socket_address;
    //snip - initialize the socket_address' members - snip//
    int status = 1;
    ioctlsocket(sock, FIONBIO, &status);  //set the socket in non-blocking mode
    while (1)
    {
       int ret = connect(sock, (sockaddr*)(&socket_address), sizeof(socket_address));
       if (ret == -1)
       {
           cout << "Connection error : " << WSAGetLastError() << endl;
           Sleep(50);  //wait a bit to not hang the CPU
       }
       else
       {
           cout << "Connection succeeded" << endl;
           break;
       }
    }
    I get this output:
    WSAEWOULDBLOCK - the connect function would block, try again later
    WSAEISCONN - the socket is already connected, why are you trying to connect it again?

    (ofcourse the output is written in numbers, but I translated the error codes and added my comments on what they mean imho).

    So, what's wrong(firt it says: try again later, but later, it says that the socket is already connected)?

  2. #2
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Don't connect() with the socket again. To find out when the connection is established you can check if the socket is writable or use asynchronous sockets.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21