Linux下Socket连接超时的一种实现方法
2009-04-17 15:46
706 查看
目前各平台通用的设置套接字(Socket)连接超时的办法是:
创建套接字,将其设置成非阻塞状态。
调用connect连接对端主机,如果失败,判断当时的errno是否为EINPROGRESS,也就是说是不是连接正在进行中,如果是,转到步骤3,如果不是,返回错误。
用select在指定的超时时间内监听套接字的写就绪事件,如果select有监听到,证明连接成功,否则连接失败。
以下是Linux环境下的示例代码:
创建套接字,将其设置成非阻塞状态。
调用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]# |
相关文章推荐
- Linux下Socket连接超时的一种实现方法
- Linux下Socket连接超时的一种实现方法
- Linux下Socket连接超时的一种实现方法
- Linux下Socket连接超时的一种实现方法(转载)
- Linux下Socket连接超时的两种实现方法 setsockopt
- linux socket编程实现connect超时的一种方法(非阻塞)
- Linux检测TCP连接断开的一种简单实现方法
- linux网络编程之socket(十一):套接字I/O超时设置方法和用select实现超时
- linux socket 设置连接超时的方法
- Linux 中连接超时的另外一种实现
- linux网络编程之socket(十一):套接字I/O超时设置方法和用select实现超时
- 一种解决Android进行蓝牙连接时发生socket已关闭或超时错误的方法
- linux网络编程之socket(十一):套接字I/O超时设置方法和用select实现超时
- Linux检测TCP连接断开的一种简单实现方法http://blog.csdn.net/wtewrw/article/details/6547380
- Linux下Socket连接超时的一种实现方法
- Linux检测TCP连接断开的一种简单实现方法
- select方法设置socket 连接超时
- 2.3 linux实现服务端与多个客户端间通讯-线程Socket-pthread的方法(实验一)
- Linux socket编程入门及客户端服务器端通信实现 – 提高篇:TCP连接过程分析
- linux网络编程之socket(十一):套接字I/O超时设置方法和用select实现超时