方法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));
TCP 怎么设置接收消息超时
最新推荐文章于 2023-07-12 16:53:58 发布