linux和windows下用setsockopt设置send(), recv() 超时的参数的一点区别

linux:

   

 1 struct  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));

你可能感兴趣的:(linux和windows下用setsockopt设置send(), recv() 超时的参数的一点区别)