您可以使用timeval 结构为任何套接字操作(不同于连接)设置超时并调用setsockopt。
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
if ( setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
sizeof(timeout)) < 0)
error( "setsockopt failed\n");
SO_RCVTIMEO选项用于表示输入操作。
您还可以使用超时参数对非阻塞套接字进行select 调用:
const int timeout_msecs = 5000;
struct timeval tval;
tval.tv_usec = 1000 * (timeout_msecs % 1000);
tval.tv_sec = timeout_msecs / 1000;
fd_set waitSet;
FD_ZERO( &waitSet );
FD_SET( fd, &waitSet );
int ret;
ret = select( fd + 1, &waitSet, NULL, NULL, &tval );
最后,Richard Stevens 在他的“Unix 网络编程”中展示的技术涉及系统中断的使用:
static void
sig_alrm(int signo)
{
return; /* just interrupt the recvfrom() */
}
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
Signal(SIGALRM, sig_alrm);
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
alarm(5);
// call recvfrom with 5 seconds timeout
if ( (n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL)) < 0) {
if (errno == EINTR)
fprintf(stderr, "socket timeout\n");
else
err_sys("recvfrom error");
} else {
alarm(0);
recvline[n] = 0; /* null terminate */
Fputs(recvline, stdout);
}
}
}