设置socket connect超时时间的几种方法

BSD

    int connectTimeout = 30;
    setsockopt(sock, IPPROTO_TCP, TCP_CONNECTIONTIMEOUT,(char *)&connectTimeout, sizeof(connectTimeout));

Linux

socket选项TCP_SYNCNT可以控制TCP连接SYN重传次数,默认为0,为0时SYN重传次数由系统参数 net.ipv4.tcp_syn_retries 控制,该系统参数默认值为6。

SYN重传次数影响connect超时时间,当重传次数为6时,超时时间为1+2+4+8+16+32+64=127秒。

    int syncnt = 4;
    setsockopt(sock, IPPROTO_TCP, TCP_SYNCNT, &syncnt, sizeof(syncnt));

使用select(Windows,Linux,BSD都可以用)

  1. 创建socket,将socket设置为非阻塞模式。
  2. 调用connect连接,如果能立即连接则返回0,不能立即连接返回-1,这个时候判断错误码是否表示暂时不能完成,是的话继续下一步。
  3. 接着调用select()在指定的时间内检测socket是否可写,如果可写表明connect()连接成功,0表示超时,-1表示出现了错误。

windows下代码:

void attemptConnect(const char* ip,unsigned short port,int timeout)
{
	//初始化
	WSADATA wsaData;
	if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) {
		printf("WSAStartup function failed\n");
		return;
	}

	SOCKET connectSocket= INVALID_SOCKET;
	do {
		//创建socket
		connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (connectSocket == INVALID_SOCKET) {
			printf("socket function failed with error: %ld\n", WSAGetLastError());
			break;
		}

		//socket设置为非阻塞 
		unsigned long on = 1;
		if (ioctlsocket(connectSocket, FIONBIO, &on) < 0) {
			printf("ioctlsocket failed\n");
			break;
		}

		//尝试连接
		sockaddr_in clientService;
		clientService.sin_family = AF_INET;
		clientService.sin_addr.s_addr = inet_addr(ip);
		clientService.sin_port = htons(port);
		int ret = connect(connectSocket, (struct sockaddr*)&clientService, sizeof(clientService));
		if (ret == 0) {
			printf("connect success1\n");
			return;
		}

		//因为是非阻塞的,这个时候错误码应该是WSAEWOULDBLOCK,Linux下是EINPROGRESS
		if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
			printf("connect failed with error: %ld\n", WSAGetLastError());
			return;
		}


		fd_set writeset;
		FD_ZERO(&writeset);
		FD_SET(connectSocket, &writeset);
		timeval tv;
		tv.tv_sec = timeout;
		tv.tv_usec = 0;
		ret = select(connectSocket + 1, NULL, &writeset, NULL, &tv);
		if (ret == 0) {
			printf("connect timeout\n");
		} else if (ret < 0) {
			printf("connect failed with error: %ld\n", WSAGetLastError());
		} else {
			printf("connect success2\n");
		}
	} while (false);
	if (connectSocket != INVALID_SOCKET) {
		closesocket(connectSocket);
	}
	WSACleanup();
}
 
int main()
{
	attemptConnect("127.0.0.1",80,15);
	system("pause");
	return 0;
}

已标记关键词 清除标记
我的本意是通过select超时来快速返回socket失败的问题,代码如下 但是我现在的IP都是不可用的ping不通,为什么select总是返回1 呢? ``` /****************************** * Time out for connect() * Write by Kerl W ******************************/ #include <stdio.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/time.h> #include <netinet/in.h> #include <sys/ioctl.h> #include <fcntl.h> #include <errno.h> #include <time.h> #define TIME_OUT_TIME 10 //connect超时时间20秒 #define true 1 #define false 0 int main(int argc , char **argv) { int flags, error = 0,len,ret; struct sockaddr_in serv_addr; int sockfd = socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) exit(1); //以服务器地址填充结构serv_addr len = sizeof(int); struct timeval tm; fd_set set; unsigned long ul = 1; //设置sockaddr_in结构体中相关参数 serv_addr.sin_family = AF_INET; //地址族 IPV4 serv_addr.sin_port = htons(80); //设置为要连接的服务器的端口号(short数据转化为网络数据) serv_addr.sin_addr.s_addr = inet_addr("192.168.3.64"); //设置服务器的IP地址(字符串转化为整形) // ioctl(sockfd, FIONBIO, &ul); //设置为非阻塞模式 flags = fcntl(sockfd, F_GETFL, 0); if(fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) < 0){ //close(m_socket); return -2; } if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) <0) { if (errno != EINPROGRESS){ printf("errno != EINPROGRESS\n"); return -1; } tm.tv_sec == TIME_OUT_TIME; tm.tv_usec = 0; FD_ZERO(&set); FD_SET(sockfd, &set); ret = select(sockfd+1, NULL, &set, NULL, &tm); if( ret > 0) { printf("ret is %d fd_set = %d sockfd = %d\n",ret,sizeof(fd_set),sockfd); // getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len); len = sizeof(error); if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0){ return -1; /* Solaris pending error */ } } else if(ret == 0) { close(sockfd); printf( "Connect time out !\n"); return -1; } /* 恢复套接字的文件状态标志并返回 */ fcntl(sockfd, F_SETFL, flags); /* restore file status flags */ if (error) { close(sockfd); /* just in case */ errno = error; return(-2); } printf( "Connected!\n"); } } ```
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页