Read-Timeout with Blocking Socket
Read-Timeout with Blocking Socket
- Subject: Read-Timeout with Blocking Socket
- From: Mark Gilbert <email@hidden>
- Date: Sun, 18 Feb 2007 22:03:47 +0000
Folks.
I have been trying to make a timeout mechanism work for a simple
Posix socket connect or blocking read, running in OSX Tiger.
The documentation I have found (in the Unix network programming
'bible') suggests that the technique should go something like:
static void connect_alarm(int signo)
{
fprintf(stderr, " Timed out : %d\n",signo);
return;
}
typedef void Sigfunc(int); /* for signal handlers */
int read_timeout(int thesockfd, Ptr buffer, int readCount, int nsec)
{
Sigfunc *sigfunc;
int n;
int messageSize;
sigfunc = signal(SIGALRM, connect_alarm);
if (alarm(nsec) != 0)
{
// err_msg("connect_timeout: alarm was already set");
}
messageSize = read(thesockfd, buffer, readCount); // first read
alarm(0);
signal(SIGALRM, (void(*)(int))sigfunc);
return (messageSize);
}
However, in my tests, the alarm does not happen, and when the other
end of the socket fails to deliver and data to the read, the read
never times out.
Same goes for the same type of thing on an connect(). I also tried
using the setitimer instead of alarm, but same results. This is
typically calling from the main thread of an app, incidentally. The
app makes infrequent, brief requests for data from a server-
connect, write, then wait for the reply (which is this read). In
the case that the server accepts the connection and the write, but
for some reason does not reply with any data, the read sits waiting
indefinitely.
I am sure I must be missing something really basic, so perhaps
someone could advise. I could use a polled non blocking read, but
its part of a much larger intensive IP infrastructure with many
clients, and I would like to be as efficient as possible.
Thanks for any advice.
--
email@hidden
Tel: +44 208 340 5677
fax: +44 870 055 7790
http://www.gallery.co.uk
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden