SO_RCVTIMEO, SO_SNDTIMEO介紹
套接字選項SO_RCVTIMEO: 用來設置socket接收數據的超時時間;
套接字選項SO_SNDTIMEO: 用來設置socket發送數據的超時時間;
比如,一般情況下,調用accept/connect/send/recv, 進程會阻塞,但是如果對端異常,進行可能無法正常退出等待。如何讓這些調用自動定時退出?
可以使用諸如alarm定時器、I/O復用設置定時器,還可以使用socket編程里函數級別的socket套接字選項SO_RCVTIMEO和SO_SNDTIMEO,僅針對與數據接收和發送相關,而無需設置專門的信號捕獲函數。
能夠作用的系統調用包括:send、sendmsg、recv、recvmsg、accept、connect。
系統調用 | 有效選項 | 系統調用超時后的行為 |
---|---|---|
send | SO_SNDTIMEO | 返回-1,設置errno為EAGAIN或EWOULDBLOCK |
sendmsg | SO_SNDTIMEO | 返回-1,設置errno為EAGAIN或EWOULDBLOCK |
recv | SO_RCVTIMEO | 返回-1,設置errno為EAGAIN或EWOULDBLOCK |
recvmsg | SO_RCVTIMEO | 返回-1,設置errno為EAGAIN或EWOULDBLOCK |
accept | SO_RCVTIMEO | 返回-1,設置errno為EAGAIN或EWOULDBLOCK |
connect | SO_SNDTIMEO | 返回-1,設置errno為EINPROGRESS |
注意:
- EAGAIN通常和EWOULDBLOCK是同一個值;
- SO_RCVTIMEO, SO_SNDTIMEO不要求系統調用對應fd是非阻塞(nonblocking)的,但是使用了該套接字選項的sock fd,會成為nonblocking(即使之前是blocking)的。參見man手冊ERRORS EAGAIN/EWOULDBLOCK的描述;
man send關於EAGAIN / EWOULDBLOCK描述:
EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and the requested operation would block. POSIX.1-2001 allows either error to be returned for this case, and does not require these constants to have the same value, so a portable application should check for both possibilities.
示例1:設置connect超時時間
根據系統調用accept的返回值,以及errno判斷超時時間是否已到,從而決定是否開始處理超時定時任務。
客戶端程序:超時連接服務器
/**
* 客戶端程序
* 連接服務器,超時報錯、返回
* build:
* $ gcc timeout_connect.c
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
/* 超時連接 */
int timeout_connect (const char *ip, int port, int time)
{
int ret = 0;
struct sockaddr_in servaddr;
printf("client start...\n");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &servaddr.sin_addr);
servaddr.sin_port = htons(port);
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd >= 0);
/* 通過選項SO_RCVTIMEO和SO_SNDTIMEO設置的超時時間的類型時timeval, 和select系統調用的超時參數類型相同 */
struct timeval timeout;
timeout.tv_sec = time;
timeout.tv_usec = 0;
socklen_t len = sizeof(timeout);
ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, len);
if (ret == -1) {
perror("setsockopt error");
return -1;
}
if ((ret = connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))) < 0) {
/* 超時對於errno 為EINPROGRESS. 下面條件如果成立,就可以處理定時任務了 */
if (errno == EINPROGRESS) {
perror("connecting timeout, process timeout logic");
return -1;
}
perror("error occur when connecting to server\n");
}
return sockfd;
}
int main(int argc, char *argv[])
{
if (argc <= 2) {
printf("usage: %s ip_address port_number\n", argv[0]);
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
printf("connect %s:%d...\n", ip, port);
int sockfd = timeout_connect(ip, port, 10);
if (sockfd < 0) {
perror("timeout_connect error");
return 1;
}
return 0;
}
運行結果(隨意輸入一個服務器IP、端口):
$ ./timeout_connect 192.168.0.105 8000
connect 192.168.0.105:8000...
client start...
connecting timeout, process timeout logic: Operation now in progress
timeout_connect error: Operation now in progress
可以看到,本來阻塞的connect調用,10秒后返回-1,並且errno設置為EINPROGRESS。
示例2:超時接收(服務器數據)
服務器端
監聽本地任意IP地址,端口8001
從鍵盤輸入一行數據,就發送給用戶;如果沒有數據,就阻塞。
/**
* 服務器程序
* 示例:超時接收服務器數據,超時時間例程中設置為10秒
* 編譯: $ gcc timeout_recv_server.c -o server
* 運行方式:
* $ ./server
* 默認監聽端口8001(根據實際情況修改)
* 服務器功能:從鍵盤接收用戶輸入,每接收一行就向客戶輸出一行。如果沒有用戶輸入,
* 則阻塞。
* 客戶端需要跟服務器安裝在同一網段上,為了測試方便,就直接都安裝到同一機器上
*/
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <signal.h>
int sockfd = -1;
void sig_func(int sig_no)
{
if (sig_no == SIGINT || sig_no == SIGTERM) {
if (sockfd >= 0) {
close(sockfd);
}
exit(1);
}
}
int main()
{
struct sockaddr_in servaddr, cliaddr;
int listenfd;
signal(SIGINT, sig_func);
printf("server start...\n");
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
sockfd = listenfd;
int on = 1;
if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
perror("setsocketopt error");
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
// servaddr.sin_addr.s_addr = INADDR_ANY;
inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
servaddr.sin_port = htons(8001);
if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind error");
exit(1);
}
if (listen(listenfd, 5) < 0) {
perror("listen error");
exit(1);
}
char buf[1024];
socklen_t clilen = sizeof(cliaddr);
int connfd;
if ((connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen)) < 0) {
perror("accept error");
exit(1);
}
printf("input a line string: \n");
int nbytes;
while (fgets(buf, sizeof(buf), stdin)) {
nbytes = send(connfd, buf, strlen(buf), 0);
if (nbytes < 0) {
perror("send error");
break;
}
else if (nbytes == 0) {
}
printf("send: %s\n", buf);
}
close(connfd);
close(listenfd);
return 0;
}
客戶端
設置10秒超時,接收服務器數據。
客戶端10秒以內,接收到服務器數據,則直接打印;超過10秒,就報錯退出。
/**
* 客戶端程序
* 示例:超時接收服務器數據,超時時間例程中設置為10秒
* 編譯: $ gcc timeout_recv_client.c -o client
* 運行方式:
* 如本地運行(對應服務器實際監聽的IP地址和端口號) $ ./client 127.0.0.1 8001
*/
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
int timeout_recv(int fd, char *buf, int len, int nsec)
{
struct timeval timeout;
timeout.tv_sec = nsec;
timeout.tv_usec = 0;
printf("timeout_recv called, timeout %d seconds\n", nsec);
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
perror("setsockopt error");
exit(1);
}
int n = recv(fd, buf, len, 0);
return n;
}
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("usage: %s <ip address> <port>\n", argv[0]);
}
char *ip = argv[1];
uint16_t port = atoi(argv[2]);
printf("client start..\n");
printf("connect to %s:%d\n", ip, port);
int sockfd;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket error");
exit(1);
}
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, ip, &servaddr.sin_addr);
servaddr.sin_port = htons(port);
int connfd;
if ((connfd = connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr))) < 0) {
perror("connect error");
exit(1);
}
printf("success to connect server %s:%d\n", ip, port);
printf("wait for server's response\n");
char buf[100];
while (1) {
int nread;
nread = timeout_recv(sockfd, buf, sizeof(buf), 10);
if (nread < 0) {
perror("timeout_recv error");
exit(1);
}
else if (nread == 0) {
shutdown(sockfd, SHUT_RDWR);
break;
}
write(STDOUT_FILENO, buf, nread);
}
return 0;
}
客戶端運行結果:
可以看到,超過10秒后,客戶端自動退出程序,而不再阻塞在recv。
$ ./client 127.0.0.1 8001
client start..
connect to 127.0.0.1:8001
success to connect server 127.0.0.1:8001
wait for server's response
timeout_recv called, timeout 10 seconds
hello # 服務器端用戶輸入數據
timeout_recv called, timeout 10 seconds
nihao # 服務器端用戶輸入數據
timeout_recv called, timeout 10 seconds
timeout_recv error: Resource temporarily unavailable # 服務器端超時未輸入數據,客戶端程序運行結束
參考
[1]游雙. Linux高性能服務器編程[M]. 機械工業出版社, 2013.