Solved

Timeout

Posted on 2000-02-19
2
2,017 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i create updater to My Activex application? 3 79
Macro to save highlighted rows as a separate file 5 41
sum13 challenge 24 90
Unix Command -- Challenging  question 7 87
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now