Solved

Timeout

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

12 Experts available now in Live!

Get 1:1 Help Now