I send a UDP packet by sendto, then receive the answer by recv.if recv does not receive the reply, the program does not proceed. However, the udp packet might be lost, or for some reason, the packet may not be delivered, so that the program gets stuck on recv line. I wonder how is it possible to set a timeout for recv if nopacket arrives in ,e.g., a minute, then skip that line and proceed the code?
I am not pasting the full code since it is a generic udp code and my question is related to the only recv. For the final note, the development environment is linux.
unsigned long buf[maxlen];
struct protoent *proto; //
struct sockaddr_in server_addr;
int s; // socket
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));
recv(s,buf,sizeof(buf),0);