Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing UDP client server application using VC++, I can read data using recvfrom

C++
//this is a blocking call
	        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)	        {
	            die("recvfrom()");
	        }


How I can reduce this infinite waiting for a data to a fixed seconds of time ?
Posted
Comments
Sergey Alexandrovich Kryukov 5-Mar-13 18:10pm    
Why? Better do it in a separate thread...
—SA
Arun Kumar K S 5-Mar-13 23:13pm    
How I can stop that thread when no data ?

That waiting thread?

1 solution

Don't use the threads unless you really have to.
You have a several alternatives.
I suggest to use select() or poll() functions.
select() also appears in windows sockets API and has a syntax similar to BSD API.
So this one could be the most universal in simple cases.
C++
struct fd_set fds = { 0 }; // will be checked for being ready to read
FD_ZERO(&fds);
FD_SET(s, &fds);

// sets timeout
struct timeval tv = { 0 };
tv.tv_sec = TIMEOUT;
tv.tv_usec = 0;

int ret = select( s + 1, &fds, NULL, NULL, &tv );
if (  ret < 0 )
{
    // error on select()
}
else if ( ret == 0 )
{
    // timeout
    // possible function return or loop breakage
}
else if ( FD_ISSET( s, &fds ) ) // data to read?
{

    //this is a blocking call
    if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
    {
        die("recvfrom()");
    }

}

But maybe, instead of writing a recvfrom-like function with time-out you may want to redesign the application. Use non-blocking API or use select()/poll() or smth else in the main loop
(do research, there are multiple choices).

On UNIX/Linux you can use also SIGALRM eg.:
C++
signal(SIGALRM, sig_alrm);
alarm(TIMEOUT);
if ( recvfrom( ... ) < 0 )
    if ( errno == EINTR )
        // timed out
    else
       // other error
else
  // no error
  alarm(0);

See manual for distinct functions.
Hope it will help you!
Regards!

P.S.
I overlooked that you are using VC++. select() based solution works fine.
You would rather don't find alarm() and sigaction() helpful. In Windows there are WaitForMultipleObjects or IO Completion Ports for creating high performance servers.
It is very interesting issue. I encourage you to explore this topic if you want to write a professional server software.
Quick search and I've found, for instance, this article:
IP Multicasting - Using I/O completion ports with UDP[^]
 
Share this answer
 
v3
Comments
Arun Kumar K S 7-Mar-13 2:47am    
Thanks for your great solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900