Winsock C ++连接超时

时间:2017-09-05 00:30:31

标签: c++ winsock connection-timeout

我正在尝试为connect()函数设置自己的时间。

我的代码适用于默认连接,如下所示:

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;
}

我理解阻止而不阻止连接的想法,并且我找到了设置为非阻塞模式和超时的解决方案。它总是成功完成,但沟通不起作用。

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;
}

请帮助使其工作,或找到另一种解决方案(多线程在我的情况下不可用)。谢谢。

1 个答案:

答案 0 :(得分:1)

您的任何一个函数都没有检查错误的任何返回值。在非屏蔽模式下调用select()时,仅在connect()WSAEWOULBLOCK错误而失败时调用它,如果select()则返回&gt; 0然后您应首先检查setE而不是setW

尝试更像这样的事情:

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;
}