Read-Timeout with Blocking Socket
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com Folks. 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); } Thanks for any advice. -- mark@gallery.co.uk 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 (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... 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: 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. This email sent to site_archiver@lists.apple.com
participants (1)
-
Mark Gilbert