socket的阻塞模式和非阻塞模式(send和recv函数在阻塞和非阻塞模式下的表现)
python socket recv超时_socket为send和recv设置超时时间
1.TCP怎么设置接受消息超时
方法1,select函数
struct timeval tv={3,0}; //3s超时
fd_set fdset;
int result;
FD_ZERO(&fdset);
FD_SET(sock_fd, &fdset);
result = select(sock_fd + 1, &fdset, NULL, NULL, &tv);
if (result <= 0)
{
// 出错或超时
}
else
{
// 有数据包到来,可以调用recv。
}
方法2,setsockopt函数设置接收超时。
struct timeval tv = {3,0};//3s 超时
setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));