Solved

Timeout

Posted on 2000-02-19
2
2,018 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
2 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 150 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

809 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