Bootstrap

accept与connect的超时设置

connect超时:

我们都知道,connect对应与三次握手中的第一次发送SYN,而对待服务器的ACK,如果服务器没有启动服务器,有些机器会立刻返回一个RST表示服务器拒绝,从而connect失败,但又些服务器为了防止攻击,什么也不发送,直至客户端connect超时,而这一时间又75s,对于客户端来说是不能接收的。所以需要设置connect的超时时间。

利用select设置connect的超时时间:

<span style="white-space:pre">	</span>uint32_t ul = 1;
	ioctl(sock, FIONBIO, &ul);//设置成非阻塞
	printf("start time: %u\n", time(0));
	bool success = false;
	if (connect(sock, (sockaddr *)&srv_addr, sizeof srv_addr ) == -1)
	{
<span style="white-space:pre">		</span>struct timeval tm;
		tm.tv_sec = 3;
		tm.tv_usec = 0;
<span style="white-space:pre">		</span>fd_set set;
		FD_ZERO(&set);
		FD_SET(sock, &set);
		if (select(sock + 1, NULL, &set, NULL, &tm) > 0)
		{
			int error = 0;
			int len = sizeof(int);
			getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len);
			if (0 == error) success = true;
		}
		else
		{
			success = false;
		}
	}
	ul = 0;
	ioctl(sock, FIONBIO, &ul); //设置成阻塞

accept超时:

一半来说服务器应该都是在accept状态,也就无需设置accept超时时间,但一些特殊情况还是需要的。对于accept我们可以使用select多路复用来实现超时设置,但那个代码较多。那有没有别的办法呢?我们指定,accept对应三次握手中中服务器接收SYN,那么我们能不能通过设置recv超时来实现accept超时呢?答案是肯定的。

struct timeval timeout = {6,0};
if (setsockopt(proxy_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(struct timeval)) != 0)
{
	printf("set accept timeout failed");
}
sock = ::accept(proxy_sock, (sockaddr *)NULL, NULL);

既然可以用recv超时来设置accept超时,那对于connect能不能通过设置send超时来实现connect超时呢?答案是肯定的!!!

setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(struct timeval));
只要将connect和accept对应到三次握手过程中就不难理解这些了。

综上:

我们可以通过设置SO_RECVTIMEO和SO_SENDTIMEO来设置accept超时和connect超时。



;