连接(功能)错误时正在进行操作错误

时间:2011-06-01 14:03:29

标签: c sockets network-programming

我想设置函数connect的超时值,但我收到此错误:“正在进行操作”

我的代码:

if ((he = gethostbyname(authdefhost)) == NULL) {
        snprintf(errbuf, CERRBUFSIZ - 1, "cannot resolve %s: %s\n", authdefhost, hstrerror(h_errno));
        return -1;
}

sin.sin_family = AF_INET;
memcpy(&sin.sin_addr, he->h_addr_list[0], sizeof(struct in_addr));       

if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        snprintf(errbuf, CERRBUFSIZ - 1, "cannot create client socket: %s\n", strerror(errno));
        return -1;
}

if ((fcntl(sd, F_SETFL, O_NONBLOCK) < 0))
    printf("error on setting socket flags.");

if (connect(sd, (void *) & sin, sizeof(sin)) == -1) {
        snprintf(errbuf, CERRBUFSIZ - 1, "cannot connect to server %s: %s\n", authdefhost, strerror(errno));
        close(sd);
        return -1;
}

FD_ZERO(&fdset);
FD_SET(sd, &fdset);
int rv;
if ((rv = select(sd + 1, NULL, &fdset, NULL, &tv)) == -1) {
    printf("error occurred on select function.");
    return -1;
}
else if (rv == 0) {
    printf("time out occurred.");
    return -1;
}
else {
    printf("connection established");
    return sd;
}

1 个答案:

答案 0 :(得分:27)

当您在非阻塞套接字上调用connect()时,您将获得EINPROGRESS而不是阻止等待连接握手完成。然后,您必须select()表示可写性,并检查套接字错误以查看连接是否已完成。

来自Linux的connect() manpage

EINPROGRESS

    The socket is nonblocking and the connection cannot be completed
    immediately.  It is possible to select(2) or poll(2) for completion by
    selecting the socket for writing.  After select(2) indicates
    writability, use getsockopt(2) to read the SO_ERROR option at level
    SOL_SOCKET to determine whether connect() completed successfully
    (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual
    error codes listed here, explaining the reason for the failure).