我有一个使用阻塞 connect() 调用的现有多线程应用程序。
但是,我想为应用程序引入连接超时,如果服务器在 x 毫秒内没有响应我们的查询,应用程序将停止尝试并给出错误。
但是,我无法弄清楚如何使用民意调查来做到这一点。
@caf 的使用 select 的非阻塞连接非常有帮助。但是与民意调查相比,我读到该选择的速度很慢,因此我想使用民意调查。你能告诉我这是否属实吗?
我从这里的帖子中粘贴他的代码
int main(int argc, char **argv) {
u_short port; /* user specified port number */
char *addr; /* will be a pointer to the address */
struct sockaddr_in address; /* the libc network address data structure */
short int sock = -1; /* file descriptor for the network socket */
fd_set fdset;
struct timeval tv;
if (argc != 3) {
fprintf(stderr, "Usage %s <port_num> <address>\n", argv[0]);
return EXIT_FAILURE;
}
port = atoi(argv[1]);
addr = argv[2];
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(addr); /* assign the address */
address.sin_port = htons(port); /* translate int2port num */
sock = socket(AF_INET, SOCK_STREAM, 0);
fcntl(sock, F_SETFL, O_NONBLOCK);
connect(sock, (struct sockaddr *)&address, sizeof(address));
FD_ZERO(&fdset);
FD_SET(sock, &fdset);
tv.tv_sec = 10; /* 10 second timeout */
tv.tv_usec = 0;
if (select(sock + 1, NULL, &fdset, NULL, &tv) == 1)
{
int so_error;
socklen_t len = sizeof so_error;
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
if (so_error == 0) {
printf("%s:%d is open\n", addr, port);
}
}
close(sock);
return 0;
}
你能帮我用 poll 写类似的功能吗?
我在 RHEL 上并使用 gcc 4.5.x 版本。
更新:对于当前代码,一旦应用程序创建与服务器的连接,如何将套接字更改为阻塞模式。我无法找到取消设置此 O_NONBLOCK 的方法。
更新 2:fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) & ~O_NONBLOCK);
一篇文章指出我们可以使用上述命令执行此操作。虽然没有登录。