1. struct timeval tv;
2. int ret;
3. tv.tv_sec = 10;
4. tv.tv_usec = 0;
5. if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv))<0){
6. printf("socket option SO_RCVTIMEO not support\n");
7. return;
8. }
9. if (( ret = recvfrom(s, buf, sizeof buf, 0, NULL,NULL)) < 0)
10. {
11. if(ret == EWOULDBLOCK || ret== EAGAIN )
12. printf("recvfrom timeout\n");
13. else
14. printf("recvfrom err:%d\n",ret);
15. }