mjr@decuac.dec.com (Marcus J. Ranum) (05/10/91)
I've been trying to implement what amounts to a timeout on a connect(2) call, and I'm unsatisfied with the results I've been getting. Basically, I set the socket nonblocking, do the connect(), it fails, then I select the socket for reading. First off, I get EINPROGRESS instead of EWOULDBLOCK as TFM implies, but worst of all I don't have an indication of whether or not the file descriptor is any good - in the case where I select on the fd, and a very short time later I get a connection refusal, it's hard to tell if that indeed happened. What's the elegant way to do this? I'd basically like to just "connect to that machine and give me an error if it takes more than N sec" kind of thing. Do I have to do something really gross like try to write junk and see if it bombs? TFM for fstat implies it's useless. Suggestions? mjr.
subbarao@phoenix.Princeton.EDU (Kartik Subbarao) (05/10/91)
In article <1991May09.221038.21861@decuac.dec.com> mjr@decuac.dec.com (Marcus J. Ranum) writes: > > > I've been trying to implement what amounts to a timeout on a >connect(2) call, and I'm unsatisfied with the results I've been getting. >Basically, I set the socket nonblocking, do the connect(), it fails, >then I select the socket for reading. First off, I get EINPROGRESS >instead of EWOULDBLOCK as TFM implies, but worst of all I don't have >an indication of whether or not the file descriptor is any good - >in the case where I select on the fd, and a very short time later I >get a connection refusal, it's hard to tell if that indeed happened. Wow, this is amazing. I have been working on the *exact* same thing. This has to be more than just plain net.coincidence! Okay, on to real stuff. Are you sure about your FM? My FM for connect(2) (SunOS 4.1.1) says that the error returned is EINPROGRESS. > What's the elegant way to do this? I'd basically like to just >"connect to that machine and give me an error if it takes more than N sec" >kind of thing. Do I have to do something really gross like try to write >junk and see if it bombs? TFM for fstat implies it's useless. Yeah, that's basically what I've been doing. Execpt for me, it's not really gross because that's the purpose of connecting to a particular machine; to read some inforamation. I make the socket nonblocking, then connect() (in which almost all cases returns -1 immediately with EINPROGRESS), then select() on the fd for a second. If the select returns 1, that means that either the socket is ready to be dealth with, or that the connection went bad. Since at this stage of the game I'm only worried about not timing out for 30 seconds just waiting, I just go on. Later, when I try to read from the socket or write to it, if it returns -1, I know exactly (well, pretty much) what the problem was and I go on. I guess there should be a better way to do this, but this doesn't overinconvenience me too much. -Kartik -- internet% ypwhich subbarao@phoenix.Princeton.EDU -| Internet kartik@silvertone.Princeton.EDU (NeXT mail) SUBBARAO@PUCC.BITNET - Bitnet