mjr@decuac.dec.com (Marcus J. Ranum) (02/07/91)
Someone a few days ago was asking if there's a way to set a timeout on a connect(2) call - similarly to the timeouts you can set in a select(2). Did anyone have an answer to this? Turns out I need to do something similar. TFM says if you try to connect on a nonblocking socket, you'll get an EWOULDBLOCK if it would block - but I'd like to be able to specify the time a little more precisely. Is there an elegant way to do this? mjr.
gruner@Informatik.TU-Muenchen.DE (Armin Gruner) (02/07/91)
In article <1991Feb06.181726.2735@decuac.dec.com>, mjr@decuac.dec.com (Marcus J. Ranum) writes: |> |> Someone a few days ago was asking if there's a way to set a timeout |> on a connect(2) call - similarly to the timeouts you can set in a select(2). |> Did anyone have an answer to this? Turns out I need to do something similar. |> TFM says if you try to connect on a nonblocking socket, you'll get an |> EWOULDBLOCK if it would block - but I'd like to be able to specify the time |> a little more precisely. Is there an elegant way to do this? |> |> mjr. Hello.. You may use a nonblocking socket, so you will get rc=-1 and errno=EINPROGRESS if the connection cannot be made immediately. Then, use select(2) to see when the connection has been established. If this doesn't work, you may try the following: dummy(s) { /* ** Don't ignore the interrupt, but just do nothing, maybe you will have to ** set the handler again */ } /* ** If your system has restarting system calls, you may need: ** siginterrupt(SIGALRM, 1); */ signal(SIGALRM, dummy); alarm(TIMEOUT); rc = connect(...); alarm(0); The connect() will return rc=-1 and errno=EINTR. Greetings, Armin.