2

I'am trying to set my own timeot for connect() function.

my code works well with default connection like this:

bool connectFUNC4(char * ipaddr) {

WSADATA wsa;
struct sockaddr_in server;

if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
return false;

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
return false;

server.sin_addr.s_addr = inet_addr(ipaddr);
server.sin_family = AF_INET;
server.sin_port = htons(5577);

if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0)
    return false;
return true;
}

I understand idea with blocking and not blocking connection, and I've found solution with setting to nonblocking mode and timeout. It always finished successful but communication does not work.

bool connectFUNC3(char * ipaddr) {

WSADATA wsa;
struct sockaddr_in server;

server.sin_addr.s_addr = inet_addr(ipaddr);
server.sin_family = AF_INET;
server.sin_port = htons(5577);

unsigned long block = 1;
ioctlsocket((unsigned int)sock, FIONBIO, &block);

WSAGetLastError();

int ret = connect(sock, (struct sockaddr *)&server, sizeof(server));
timeval time_out;
time_out.tv_sec = 5;
time_out.tv_usec = 0; 

fd_set setW, setE;

FD_ZERO(&setW);
FD_SET(sock, &setW);
FD_ZERO(&setE);
FD_SET(sock, &setE);
select(0, NULL, &setW, &setE, &time_out);

bool flag;

if (FD_ISSET(sock, &setW))
{
    // connection successful
    flag = true;
}
else if (FD_ISSET(sock, &setE))
{
    // connection fail
    flag = false;
}
else
{
    // connection timeout
    flag = false;
}

block = 0;
ioctlsocket((unsigned int)sock, FIONBIO, &block);
return flag;
}

Please help to make it work, or to find another solution (multithread not usable in my case). Thank you.

Dmitriy
  • 77
  • 1
  • 1
  • 5
  • 1
    You seem to have a modified version of this answer: https://stackoverflow.com/a/2597774/4581301 I recommend looking the answer over and justifying or adopting the differences. For example, you should follow their example and check the return code on `select`. – user4581301 Sep 05 '17 at 00:40
  • 'Communication does not work' is not a problem description. Try again. – user207421 Sep 05 '17 at 00:48
  • Don't work means that my code after connection send 4 byte request and recieve 14 bytes answer. with connectFUNC4 it works weel, but with FUNC3 no communication detected by wireshark. – Dmitriy Sep 05 '17 at 01:44
  • In Func3 you don't check the return value of the connect call. See the remarks under the documentation about the return value of connect: https://msdn.microsoft.com/en-us/library/windows/desktop/ms737625(v=vs.85).aspx – kvr Sep 05 '17 at 03:02
  • And what the hell are FUNC3 and connectFUNC4? – user207421 Sep 05 '17 at 11:06
  • Does this answer your question? [C: socket connection timeout](https://stackoverflow.com/questions/2597608/c-socket-connection-timeout) – Olivier Jan 12 '22 at 19:01

1 Answers1

8

Neither of your functions are checking ANY return values for errors. And when calling select() in non-blocking mode, call it only if connect() fails with a WSAEWOULBLOCK error, and if select() then returns > 0 then you should be checking setE first and not setW.

Try something more like this:

void closesock(SOCKET *s)
{
    // preserve current error code
    int err = WSAGetLastError();
    closesocket(*sock);
    *sock = INVALID_SOCKET;
    WSASetLastError(err);
}

bool connectFUNC4(char * ipaddr)
{
    // you really shouldn't be calling WSAStartup() here.
    // Call it at app startup instead...

    struct sockaddr_in server = {0};
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ipaddr);
    server.sin_port = htons(5577);

    // ipaddr valid?
    if (server.sin_addr.s_addr == INADDR_NONE)
        return false;

    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
        return false;

    if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
    {
        // connection failed
        closesock(&sock);
        return false;
    }

    // connection successful

    return true;
}

bool connectFUNC3(char * ipaddr)
{
    // you really shouldn't be calling WSAStartup() here.
    // Call it at app startup instead...

    struct sockaddr_in server = {0};
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ipaddr);
    server.sin_port = htons(5577);

    // ipaddr valid?
    if (server.sin_addr.s_addr == INADDR_NONE)
        return false;

    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock == INVALID_SOCKET)
        return false;

    // put socked in non-blocking mode...
    u_long block = 1;
    if (ioctlsocket(sock, FIONBIO, &block) == SOCKET_ERROR)
    {
        closesock(&sock);
        return false;
    }

    if (connect(sock, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR)
    {
        if (WSAGetLastError() != WSAEWOULDBLOCK)
        {
            // connection failed
            closesock(&sock);
            return false;
        }

        // connection pending

        fd_set setW, setE;

        FD_ZERO(&setW);
        FD_SET(sock, &setW);
        FD_ZERO(&setE);
        FD_SET(sock, &setE);

        timeval time_out = {0};
        time_out.tv_sec = 5;
        time_out.tv_usec = 0; 

        int ret = select(0, NULL, &setW, &setE, &time_out);
        if (ret <= 0)
        {
            // select() failed or connection timed out
            closesock(&sock);
            if (ret == 0)
                WSASetLastError(WSAETIMEDOUT);
            return false;
        }

        if (FD_ISSET(sock, &setE))
        {
            // connection failed
            int err = 0;
            getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, sizeof(err));
            closesock(&sock);
            WSASetLastError(err);
            return false;
        }
    }

    // connection successful

    // put socked in blocking mode...
    block = 0;
    if (ioctlsocket(sock, FIONBIO, &block) == SOCKET_ERROR)
    {
        closesock(&sock);
        return false;
    }

    return true;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770