0

I have problem with setting timeout to recvfrom(). I run my code under Cygwin64 environment.

.his is a sample from my code:

if(( clientSock=socket(AF_INET, SOCK_DGRAM,0))==-1){ // creat client socket 
    perror("Socket Initialisation Error\n");
}//if

struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
if (setsockopt(clientSock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof tv)) {
    perror("setsockopt");    
}

memset(&client,0, sizeof(client)); // clear the memory of 
client addrinfo

client.sin_port=htons(clientPort);// asign client port
client.sin_family=AF_INET; // ipv4
client.sin_addr.s_addr=INADDR_ANY;
bzero(&client.sin_zero,8);

if((bind(clientSock,(struct sockaddr*)&client, sizeof(struct sockaddr_in)))==-1){ // bind socket with addrinfo
    perror("\n Bind: ");
} 

server.sin_family=AF_INET; // server ipv4 type
server.sin_port=htons(serverPort); // assign server port
server.sin_addr.s_addr=inet_addr(serverIp); // asign server ip
bzero(&server.sin_zero,8);

while(1){
    // send to the server is ok then  wait for receive: 

    if((recvfrom(clientSock,&rcvPkt,sizeof(struct packet),0,(struct sockaddr *)&server,&sockLength))<0){ // wait for receiving replay from server 
        puts("\nerror in receiving CONF packet");
    }
    sleep(1);
}

The program keeps waiting on recvfrom(): the timeout is not working.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
SHADOW.NET
  • 555
  • 1
  • 5
  • 20
  • This is under Cygwin64, I tried the same code under Ubuntu and it works fine. can anyone tells me what is the problem with Cygwin ? – SHADOW.NET Feb 25 '16 at 04:58
  • On Windows, `SO_RCVTIMEO` expects a `DWORD` expressed in milliseconds, not a `timeval` struct. `SO_RCVTIMEO` is not portable, you should use `select()` instead. – Remy Lebeau Feb 25 '16 at 17:24
  • I am running the code under Linux using (Cygwin) not windows enviroment – SHADOW.NET Feb 25 '16 at 18:24
  • Cygwin is an emulation layer that brings Linux APIs to Windows, how are you using it on Linux? – Remy Lebeau Feb 25 '16 at 20:13
  • I am using Cygwin which is Linux environment . so, basically the code is being compiled under Linux – SHADOW.NET Feb 27 '16 at 01:48

0 Answers0