Solved

Timeout in UNIX

Posted on 1998-09-29
1
672 Views
Last Modified: 2010-05-18
How do you implement a timeout in Unix???
 
Let me explain what I am trying to do:
 
I've opened a device file (serial port - connected to a modem) for I/O. I am communicating with a terminal (phone) which also has a modem. I need to wait for a response from the terminal after having sent a command. So I do a 'write' to the device file to send the command to the terminal, and do a 'read' from the device to get the response. I want to wait, let us say, only 15 seconds for the response, HOW DO I DO IT?
 
I am using the standard 'read' and 'write' routines.                                
0
Comment
Question by:sanjaychopra
[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
1 Comment
 

Accepted Solution

by:
kamath earned 50 total points
ID: 2007158
there is a system call called *select* in UNIX. you can define a file descriptor set and a filter and specify a timeout:

Look out man pages for select
also here is a code that does it:
 // Reference   : C Library function select(3C)
   fd_set readfds;
   FD_ZERO(&readfds);
   FD_SET(mSocket,   &readfds);
   struct timeval timeout;
   timeout.tv_sec = mReceiveTimeout;
   timeout.tv_usec   = 0;
   int numFds =   select(mSocket+1, &readfds, 0, 0, &timeout);
   int numReceived   = 0;

   // If an error occurred, deal with it
   if (numFds == -1)
      throw(Exception(errno));
 
   // Read  the   input, if available
   else  if  (numFds  ==   1)
   {
      int ret  = recv(mSocket,    aBuf, aBufSize,   0);
      if (ret  == -1)
         throw(Exception(errno));
      else /*  if (ret  != -1) */
         numReceived = ret;
   }
 
   // Otherwise, there was a timeout
   else  if  (numFds  ==   0)
      throw(Exception(errno));

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

717 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