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

分类: 桌面应用   出处:iocblog整理  更新时间:2008-12-17   添加到收藏  

目前各平台通用的设置套接字(socket)连接超时的办法是:
 

  1. 创建套接字,将其设置成非阻塞状态。
  2. 调用connect连接对端主机,如果失败,判断当时的errno是否为einprogress,也就是说是不是连接正在进行中,如果是,转到步骤3,如果不是,返回错误。
  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 ", time(null));
        if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
                printf("connected ");
                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 ");
                printf("%d ", time(null));
                return 0;
        }
        printf("connected ");

        return 0;
}


  实际运行结果如下:

 

xiaosuo@gentux perl $ ./a.out 10.16.101.1 90
1180289276
timeout
1180289279
xiaosuo@gentux perl $ ./a.out 10.16.101.1 90 1
1180289281
timeout
1180289282
 


  可以看到,以上代码工作的很好,并且如果你想知道连接发生错误时的确切信息的话,你可以用getsocketopt获得:

 (来源 www.iocblog.net)

int error;
socklen_t errorlen = sizeof(error);

getsockopt(fd, sol_socket, so_error, &error, &errorlen);


  但是多少有些复杂,如果有象so_sndtimo/so_rcvtimo一样的套接字参数可以让超时操作跳过select的话,世界将变得更美好。当然你还可以选用象apr一样提供了简单接口的库,但我这里要提的是另一种方法。

[1] [2] 下一页


Tag: Socket



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。