Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Timeout

Posted on 2000-02-19
2
Medium Priority
?
2,025 Views
Last Modified: 2013-12-26
I am writing a echo server/client program in Unix.
I have executed the following statement to send and receive a UDP packet.

while (1) {
sendto(outFd, data, dataLength, 0, (struct sockaddr*)&outAddr, sizeof(outAddr))
int n = recvfrom(inFd, buffer, length, 0, (struct sockaddr*) &inAddr, &inAddLen
);

if (timeout)continue;
else break;
}

Since UDP is not reliable, so the recvfrom()sometimes never get back it echo. So I would like to add a timeout handle. If recvfrom() is timeout, then the program would go back to sendto() and send data again.

I know I should use signal and alarm. But what I should exactly add to my program so that when the recvfrom() time out, the signal handler would help me to "skip" the recvfrom() and do sendto() again?
0
Comment
Question by:matchz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 600 total points
ID: 2539533
matchz,

better is you do not bet on signals and alarms.You can use select() which comes handy for this purpose.in select() call you can specify the timeout in millisec resoultion.So you can wait for some time and try sendto again if select returns timeout.
fd_set RecFd,ErrFd;
const struct timeval * timeout ;

/** Put app timeout values here ***/
timeout.tv_sec=0;
timeout.tv_use=0;
/****************************/

while (1) {
                   sendto(outFd, data, dataLength, 0, (struct sockaddr*)&outAddr, sizeof(outAddr)) ;
                   /*** Code for timeout **/
                  FD_SET(inFd, &RecFd)
                  FD_SET(inFd, &ErrFd)
                  select ( inFd+1, &RecFd,NULL,&ErrFd, &timeout);
                  if( FD_ISSET(inFd, &RecFd)  //OK
                      int n = recvfrom(inFd, buffer, length, 0, (struct sockaddr*) &inAddr, &inAddLen
                 else if( FD_ISSET(inFd, &ErrFd)  //Socket problem
                      /* Take action for sock error */
                 else // Timeout happened
                     break;
            );
 


This is just outline code.You may have to check return value of select() etc for catching all failure conditions.But on a whole you can use this construct to acheive you goal.


0
 

Author Comment

by:matchz
ID: 2547369
While I am seldom to write program on Unix perform. I know very less in Unix programming.
Thank you for your answer and my program work now!
^_^
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question