我有一个阻塞套接字(至少在下面的代码中是这样的):
这是来自日志的内容:
Mar 6 10:42:04 tcpclient: fcntl ret=0, ret & O_NONBLOCK = 0
Mar 6 10:42:04 tcpclient: after select fcntl ret=0, ret & O_NONBLOCK = 0
Mar 6 10:42:14 tcpclient: authenticate: error on connect -> Operation now in progress
看起来这是一个阻塞套接字,但返回了非阻塞套接字的典型错误?Linux版本为2.6.18-308.el5。有什么想法吗?
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock < 0) {
ERROR("%s: error opening socket", __func__);
return (RESP_ERROR);
}
t.tv_sec = timeout;
t.tv_usec = 0;
int rf = fcntl(sock, F_GETFD);
ERROR("fcntl ret=%d, ret & O_NONBLOCK = %d", rf, rf & O_NONBLOCK);
if ((setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&t, sizeof (t)) < 0)
|| (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&t, sizeof (t)))) {
strerror_r(errno, err, 254);
ERROR("%s: error on setsockopt -> %s", __func__, err);
close(sock);
return (RESP_ERROR);
}
rf = fcntl(sock, F_GETFD);
ERROR("after select fcntl ret=%d, ret & O_NONBLOCK = %d", rf, rf & O_NONBLOCK);
if (connect(sock, (struct sockaddr *)&dst, sizeof (dst)) != 0) {
strerror_r(errno, err, 254);
ERROR("%s: error on connect -> %s", __func__, err);
close(sock);
return (RESP_ERROR);
}
这是来自日志的内容:
Mar 6 10:42:04 tcpclient: fcntl ret=0, ret & O_NONBLOCK = 0
Mar 6 10:42:04 tcpclient: after select fcntl ret=0, ret & O_NONBLOCK = 0
Mar 6 10:42:14 tcpclient: authenticate: error on connect -> Operation now in progress
看起来这是一个阻塞套接字,但返回了非阻塞套接字的典型错误?Linux版本为2.6.18-308.el5。有什么想法吗?
connect()
函数之前如何初始化dst
。你确定它的成员变量sin_family
被正确设置了吗? - alk