1.调用alarm() ( alarm()的返回值是timer中的剩余时间)
2.在select()处阻塞等待
3.在socket选项中设定SO_RCVTIMEO 或 SO_SNDTIMEO
但这个方法不是所有实现都支持
具体说一个函数就是在使用recvfrom()之前调用 readable_timeo( int fd, int sec)
程序代码:
int readble_timeo(int fd, int sec) {
fd_set rset;
struct timeval tv;
FD_ZERO(rset);
FD_SET(fd, &rset)
tv.tv_sec = sec;
tv.tv_usec = 0;
return (select(fd+1, &rset, NULL, NULL, &tv));
}
返回值若为-1, 表示错误
0, 表示超时
一个正数, 则表示某个descriptor 已经可读了