當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 網(wǎng)絡(luò)超時(shí)檢測(cè)的三種方法
網(wǎng)絡(luò)通信中,很多操作會(huì)使得進(jìn)程阻塞,這時(shí)我們要設(shè)定時(shí)間,到時(shí)間后強(qiáng)制返回,避免進(jìn)程在沒有數(shù)據(jù)的情況下無(wú)限阻塞
這里我們總結(jié)一下網(wǎng)絡(luò)超時(shí)檢測(cè)的三種方法:
通過setsockopt設(shè)置套接字屬性SO_RCVTIMEO
struct timeval t = {5, 0}
if (setsockopt(listenfd, SOL_SOCKET, SO_RCVTIMEO, &t, sizeof(t)) == -1) {
perror("setsockopt");
return -1;
}
memset(&peeraddr, 0, sizeof(peeraddr));
len = sizeof(peeraddr);
if ((connfd = accept(listenfd, (struct sockaddr *)&peeraddr, &len)) == -1) {
printf("errno=%d: %s\n", errno, strerror(errno));
if (errno == EAGAIN) {
printf("timeout\n");
return -1;
}
}
二、設(shè)定select函數(shù)的一個(gè)參數(shù)實(shí)現(xiàn)超時(shí)處理
struct timeval t= {3, 0};
while (1) {
。。。。。。
t.tv_sec = 3;
t.tv_usec = 0;
if ((ret = select(maxfd+1, &rdfs, NULL, NULL, &t)) == -1) {
perror("select");
return -1;
}
。。。。。。
}
三、設(shè)定一個(gè)定時(shí)器捕捉SIGALRM信號(hào)做超時(shí)控制
struct sigaction act;
sigaction(SIGALRM, NULL, &act); //獲取SIGALRM信號(hào)的屬性
act.sa_handler = handler; // 設(shè)置SIGALRM信號(hào)的處理函數(shù)
sigaction(SIGALRM, &act, NULL); // 設(shè)置SIGALRM信號(hào)的屬性
alarm(3); // 定時(shí)器設(shè)置3秒鐘
while (1) {
if ((connfd = accept(listenfd, (struct sockaddr *)&peeraddr, &len)) == -1) {
if (errno == EINTR) {
printf("timeout\n");
return -1;
}
}
定時(shí)器3秒鐘內(nèi)沒有數(shù)據(jù)到來(lái),內(nèi)核產(chǎn)生SIGALRM信號(hào)中斷當(dāng)前操作。我們知道設(shè)置信號(hào)捕捉函數(shù)可以用signal函數(shù)或是sigaction函數(shù)。但這里只能使用sigaction函數(shù),因?yàn)閟ignal設(shè)置的信號(hào)處理函數(shù)執(zhí)行完后會(huì)重新執(zhí)行被中斷的操作