linux網路程式設計二十:socket選項:SO_RCVTIMEO和SO_SNDTIMEO
SO_RCVTIMEO和SO_SNDTIMEO ,它們分別用來設定socket接收資料超時時間和傳送資料超時時間。
因此,這兩個選項僅對與資料收發相關的系統呼叫有效,這些系統呼叫包括:send, sendmsg, recv, recvmsg, accept, connect 。
這兩個選項設定後,若超時, 返回-1,並設定errno為EAGAIN或EWOULDBLOCK.
其中connect超時的話,也是返回-1, 但errno設定為EINPROGRESS
1. 程式碼:設定connect超時時間
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//超時連線
int timeout_connect(const char *ip, int port, int time);
int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: %s ip port\n", argv[0]);
return 1;
}
const char *ip = argv[1];
int port = atoi(argv[2]);
int sockfd = timeout_connect(ip, port, 10);
if (sockfd < 0)
return 1;
return 0;
}
int timeout_connect(const char *ip, int port, int time)
{
int ret = 0;
int error;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
return -1;
//超時時間
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) {
error = errno;
while ((close(sockfd) == -1) && (errno == EINTR));
errno = error;
return -1;
}
ret = connect(sockfd, (struct sockaddr*)&address, sizeof(address));
if (ret == -1) {
if (errno == EINPROGRESS) {
printf("connecting timeout\n");
return -1;
}
printf("error occur when connecting to server\n");
return -1;
}
char buffer[1024];
memset(buffer, '\0', 1024);
ret = recv(sockfd, buffer, 1024, 0);
printf("recv %d bytes, buf: %s\n", ret, buffer);
return sockfd;
}
參考:《linux高效能伺服器程式設計》
---------------------
作者:jasonliuvip
來源:CSDN
原文:https://blog.csdn.net/jasonliuvip/article/details/23567843
版權宣告:本文為博主原創文章,轉載請