Solved

Timeout

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

734 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