UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » Networking
  • » Problem while using the Select() to set the time out for 45 seconds.

#1 2012-12-11 06:37 AM

Vani
Member
Registered: 2012-12-11
Posts: 1

Problem while using the Select() to set the time out for 45 seconds.

I'm not able to set the timeout using select().
For every 15 seonds,I should recieve the data..

If data is not recieved for 45 seconds,it should return...
But its not happening,whats the problem with my code.
Code:

while(1)
	 {

         // clear the set ahead of time
         FD_ZERO(&readfds);

         // add our descriptors to the set
         FD_SET(sockId, &readfds);


         receive_timeout.tv_sec = 45;
          receive_timeout.tv_usec = 0;

        rv = select(sockId, &readfds, NULL, NULL, &receive_timeout);


		 if (rv == -1)
		 {
		     perror("select\n"); // error occurred in select()
		 }
		 else if (rv == 0)
		 {
			 timer.isTimeOut = TRUE;
		     printf("Timeout occurred!  No data after 45 seconds.\n");
		 }
		 else
		 {
		     // one or both of the descriptors have data
		     if (FD_ISSET(sockId, &readfds))
		     {
		    	 if ((nbytes = recvfrom(sockId,&buffert,sizeof(buffer),0,&clientaddr,&clientaddrlen)) < 0)
		    	         	  {
		    		               timer.isTimeOut = FALSE;
		    	 				  //log the error
		    	 				   //close(sockId);
		    	 	   }
		    	 else
		    	 {
		    		 timer.isTimeOut = FALSE;
		    	 }
		     }

		 }

sleep(15);
}
}

Last edited by Vani (2012-12-11 07:13 AM)

Offline

#2 2012-12-11 01:32 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Problem while using the Select() to set the time out for 45 seconds.

rv = select(sockId, &readfds, NULL, NULL, &receive_timeout);

You should be passing "sockId + 1" there...  The value needs to be one higher than your largest FD value...

Also, why are you doing a sleep(15) at the bottom of the loop?  That's not really sensible...  You just want to block in the select()...  Adding arbitrary sleeps will just delay your detection of new data to read...

Offline

  • Index
  • » Networking
  • » Problem while using the Select() to set the time out for 45 seconds.

Board footer

Powered by FluxBB