home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / unix / programm / 5894 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  3.4 KB

  1. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!howland.reston.ans.net!spool.mu.edu!uwm.edu!ogicse!news.u.washington.edu!glia!hawkeye
  2. From: hawkeye@glia.biostr.washington.edu (Ken Keys - TF Dude)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: asynchronously connecting a socket
  5. Message-ID: <hawkeye.726799475@glia>
  6. Date: 12 Jan 93 00:44:35 GMT
  7. Article-I.D.: glia.hawkeye.726799475
  8. References: <1icau7INNg7f@grasp1.univ-lyon1.fr>
  9. Organization: University of Washington
  10. Lines: 69
  11. NNTP-Posting-Host: glia.biostr.washington.edu
  12.  
  13. In <1icau7INNg7f@grasp1.univ-lyon1.fr> Christophe.Wolfhugel@grasp.insa-lyon.fr (Christophe Wolfhugel) writes:
  14.  
  15. >The connect(3n) system call when used aynchronously will return -1 with
  16. >errno set to EINPROGRESS.  A write select(3n) will indicate completion
  17. >of the connect.  Is this an option or is it mandatory ?
  18.  
  19. >In order to check return codes, I used a small loop:
  20.  
  21. >      while (connect(f->f_file, &ip, sizeof ip) < 0) {
  22. >         fprintf(stderr, "errno = %d\n", errno);
  23. >         sleep(1);
  24. >         continue;
  25. >      } /* endif */
  26.  
  27. >This returns:
  28.  
  29. >errno = 150 (EINPROGRESS)
  30. >errno = 11 (EAGAIN)
  31. >[many other errno = 11]
  32. >Broken pipe
  33.  
  34. >and the application dies.  Is this behavior illegal (I know it does not
  35. >make much sense to have such a loop).  Shouldn't I get a EALREADY
  36. >instead of the EAGAIN ?
  37.  
  38. >Also, in a select loop.  Once select indicates a write even for that
  39. >file description.  How do I check the connection ?  Redo a connect()
  40. >and except errno to be either EISCONN to indicate success, any other
  41. >result to indicate failure ?
  42.  
  43. >-- 
  44. >Christophe Wolfhugel    |    Email: Christophe.Wolfhugel@grasp.insa-lyon.fr
  45.  
  46. According to my man pages (on BSDi and IRIX), connect() should set errno
  47. to EALREADY if a connection is already in progress.  EAGAIN is not listed
  48. as one of the possible errno values.  But I have seen systems which can
  49. use EAGAIN in this situation, so be prepared to deal with it.
  50.  
  51. The biggest problem is finding out when the connection is ready.  The
  52. man pages claim that it is ready when the socket selects true for
  53. writing.  But I've seen systems where select *always* return true for
  54. writing, so if you're trying to write portable code, this won't work.
  55. Even if select is correct, you must test the socket to see if the
  56. connection worked.  A second connect() will set errno=EISCONN if the
  57. first one worked, and anything else indicates failure; however, there
  58. is no way of finding out *why* the original nonblocking connect() failed.
  59.  
  60. A more robust way of doing asynchronous connections is by using a
  61. seperate process as a connection server.  When a (client) process needs
  62. a connection, the server does a blocking connect() call.  When the call
  63. returns, the server passes either an error number or the open file
  64. descriptor back to the client.  Of course, your system must support
  65. open file descriptor passing (SVR4, BSD "4.3+", and other "modern"
  66. systems support this).  I implemented this method myself by having the
  67. client create a child process for each connection it needed.  The
  68. client could select on the pipe(s) to the process(es) to determine when
  69. a connection had succeeded or failed.  The book _Advanced Programming
  70. in the UNIX Environment_ by W. Richard Stevens contains an excellent
  71. description of this method.
  72.  
  73. --
  74. Ken Keys, aka Hawkeye
  75. Master of the Fugue
  76. kkeys@ucsd.edu or hawkeye@ucsd.edu
  77. -- 
  78. --
  79. Ken Keys, aka Hawkeye
  80. Master of the Fugue
  81. kkeys@ucsd.edu or hawkeye@ucsd.edu
  82.