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

iya*_*sar 15 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;
}
Run Code Online (Sandbox Code Playgroud)

nin*_*alj 32

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

从Linux的手册connect():

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).

  • 简而言之:这是预期的.你需要在`connect()`的错误检查路径中专门检查`errno!= EINPROGRESS`. (7认同)
  • *第16章**UNIX网络编程(第1卷)的非阻塞I/O*详细描述了此功能并提供演示. (2认同)