您的位置:首页 > 运维架构 > Linux

Linux下Socket连接超时的一种实现方法

2009-04-17 15:46 706 查看
目前各平台通用的设置套接字(Socket)连接超时的办法是:

创建套接字,将其设置成非阻塞状态。

调用connect连接对端主机,如果失败,判断当时的errno是否为EINPROGRESS,也就是说是不是连接正在进行中,如果是,转到步骤3,如果不是,返回错误。

用select在指定的超时时间内监听套接字的写就绪事件,如果select有监听到,证明连接成功,否则连接失败。

  以下是Linux环境下的示例代码:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <time.h>

int main(int argc, char *argv[])
{
int fd, retval;
struct sockaddr_in addr;
struct timeval timeo = {3, 0};
socklen_t len = sizeof(timeo);
fd_set set;

fd = socket(AF_INET, SOCK_STREAM, 0);
if (argc == 4)
timeo.tv_sec = atoi(argv[3]);
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(argv[1]);
addr.sin_port = htons(atoi(argv[2]));
printf("%d/n", time(NULL));
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
printf("connected/n");
return 0;
}
if (errno != EINPROGRESS) {
perror("connect");
return -1;
}
FD_ZERO(&set);
FD_SET(fd, &set);
retval = select(fd + 1, NULL, &set, NULL, &timeo);
if (retval == -1) {
perror("select");
return -1;
} else if(retval == 0) {
fprintf(stderr, "timeout/n");
printf("%d/n", time(NULL));
return 0;
}
printf("connected/n");

return 0;
}


[root@lanhai-linux CONNI]# ./coni 10.16.101.1 90
1223898556
timeout
1223898559
[root@lanhai-linux CONNI]# ./coni 10.16.101.1 90
1223898609
timeout
1223898612
[root@lanhai-linux CONNI]# ./coni 192.168.18.45
段错误
[root@lanhai-linux CONNI]# ./coni 192.168.18.45 90
1223898767
[root@lanhai-linux CONNI]# ./coni 192.168.18.37 80
1223898771
connected
[root@lanhai-linux CONNI]#

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: