Posted on 2010-09-14 16:38
杨书童 阅读(3894)
评论(0) 编辑 收藏 引用 所属分类:
基础知识-经验之谈
linux:
1struct timeval timeout={3,0};//3s
2
3 int ret=setsockopt(sock_fd,SOL_SOCKET,SO_SNDTIMEO,&timeout,sizeof(timeout));
4
5 int ret=setsockopt(sock_fd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));
6
7 如果ret==0 则为成功,-1为失败,这时可以查看errno来判断失败原因
8
9 int recvd=recv(sock_fd,buf,1024,0);
10
11 if(recvd==-1&&errno==EAGAIN)
12
13 {
14
15 printf("timeout\n");
16
17 }
18
19
windows:
int timeout = 3000; //3s
int ret=setsockopt(sock_fd,SOL_SOCKET,SO_SNDTIMEO,&timeout,sizeof(timeout));
int ret=setsockopt(sock_fd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));