歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux資訊 >> Linux文化 >> Linux平台控制Socket連接超時方法

Linux平台控制Socket連接超時方法

日期:2017/2/27 11:50:52   编辑:Linux文化

  目前各平台通用的設置套接字(Socket)連接超時的辦法是:

創建套接字,將其設置成非阻塞狀態。 調用connect連接對端主機,如果失敗,判斷當時的errno是否為EINPROGRESS,也就是說是不是連接正在進行中,如果是,轉到步驟3,如果不是,返回錯誤。 用select在指定的超時時間內監聽套接字的寫就緒事件,如果select有監聽到,證明連接成功,否則連接失敗。   以下是Linux環境下的示例代碼:

#include #include #include #include #include #include #include #include #include

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; }

  實際運行結果如下:

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獲得:

int error; socklen_t errorlen = sizeof(error);

getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &errorlen);

  但是多少有些復雜,如果有象SO_SNDTIMO/SO_RCVTIMO一樣的套接字參數可以讓超時操作跳過select的話,世界將變得更美好。當然你還可以選用象apr一樣提供了簡單接口的庫,但我這裡要提的是另一種方法。

  呵呵,引子似乎太長了點兒。讀Linux內核源碼的時候偶然發現其connect的超時參數竟然和用SO_SNDTIMO操作的參數一致:

  File: net/ipv4/af_inet.c

559 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK); 560 561 if ((1 sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 562 /* Error code is set above */ 563 if (!timeo || !inet_wait_for_connect(sk, timeo)) 564 goto out; 565 566 err = sock_intr_errno(timeo); 567 if (signal_pending(current)) 568 goto out; 569 }

  這意味著:在Linux平台下,可以通過在connect之前設置SO_SNDTIMO來達到控制連接超時的目的。簡單的寫了份測試代碼:

#include #include #include #include #include #include

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

fd = socket(AF_INET, SOCK_STREAM, 0); if (argc == 4) timeo.tv_sec = atoi(argv[3]); setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, len); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr(argv[1]); addr.sin_port = htons(atoi(argv[2])); if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { if (errno == EINPROGRESS) { fprintf(stderr, "timeout\n"); return -1; } perror("connect"); return 0; } printf("connected\n");

return 0; }

  執行結果:

xiaosuo@gentux perl $ ./a.out 10.16.101.1 90 1180290583 timeout 1180290586 xiaosuo@gentux perl $ ./a.out 10.16.101.1 90 2 1180290590 timeout 1180290592

  和設想完全一致!


Copyright © Linux教程網 All Rights Reserved