home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / programm / 5775 < prev    next >
Encoding:
Text File  |  1992-12-24  |  3.5 KB  |  68 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!gatech!udel!darwin.sura.net!jvnc.net!princeton!tex.Princeton.EDU!subbarao
  3. From: subbarao@fc.hp.com (Kartik Subbarao)
  4. Subject: Re: Question on daemon's under inetd
  5. Message-ID: <1992Dec24.161716.7183@Princeton.EDU>
  6. Originator: news@nimaster
  7. Sender: news@Princeton.EDU (USENET News System)
  8. Nntp-Posting-Host: tex.princeton.edu
  9. Reply-To: subbarao@fc.hp.com
  10. Organization: putchar('I'); for (i = 0; i < 3; i++) putchar('E');
  11. References: <1992Dec24.064005.5831@tigger.jvnc.net>
  12. Date: Thu, 24 Dec 1992 16:17:16 GMT
  13. Lines: 53
  14.  
  15. In article <1992Dec24.064005.5831@tigger.jvnc.net> aggarwal@tigger.jvnc.net (Vikas Aggarwal) writes:
  16. >In the past, I have come across several inetd daemons that are
  17. >started up by inetd when a connection on the particular port
  18. >comes in, and then *wait* for a small period of time
  19. >listening for any more incoming connections on their port before
  20. >exiting after a period of inactivity.
  21. >
  22. >To me, this seems like an excellent idea- achieves the best of both
  23. >arguments (standalone via inetd daemon-- startup time vs. inactivity).
  24. >
  25. >However (obviously), when I tried to do the same with a TCP daemon
  26. >that I wrote, the 'accept' call errored with 'socket not connected'.
  27. >I was doing an 'accept' on the file descriptor 0 handed to me by
  28. >'inetd'. Processing the first connection worked fine, but then I went
  29. >back to listening on the stdin (file desc 0 handed by inetd), the
  30. >accept() call returned immediately.
  31. >
  32. >I tried playing around with the 'wait' vs. 'nowait' in the inetd.conf
  33. >file, but that did not help. Do I have to set some option on the
  34. >stdin/socket or open another socket and bind to it (as I would do in
  35. >a standalone server) ? Can't I reuse the file descr 0 handed to me by
  36. >'inetd' ??
  37.  
  38. You can't issue an accept() on a socket given to you by inetd.
  39.  
  40. There are two different types of sockets at work here. One type of socket
  41. is the one connected to the remote end of the connection. It is used as an 
  42. endpoint of communication, to which you can read and write arbitrary data.
  43.  
  44. The other type, which I'll call a "server socket", is used to maintain a queue 
  45. of incoming connections from different hosts to that port. When you issue the 
  46. listen() call on a socket, it sets the SO_ACCEPTCON flag in it, thus enabling 
  47. you to accept() on that socket. Writing to or reading from this kind of
  48. sockets is useless. The only purpose of this socket is to return you
  49. another socket (from accept()) through which you CAN read and write to the 
  50. remote host.
  51.  
  52. Inetd sets up a bunch of these server sockets on the well known ports. When
  53. an incoming connection from another host is detected, accept() returns
  54. a socket of the "normal" (read/write type) to inetd, and inetd gives you
  55. this socket on fd's 0, 1 and 2. Note, this socket is bound to a different TCP
  56. port than the server socket, so that you can potentially have multiple remote
  57. hosts accessing your service. Here's where the wait/nowait comes in. If you
  58. specify wait in /etc/inetd.conf, inetd will NOT allow multiple instances of
  59. your daemon to run. That is, it will wait until the process is terminated
  60. before it forks off another instance. If you specify nowait, then it will
  61. fork off as many requests as it can handle, without waiting for any of them
  62. to finish. Once again, the socket that inetd returns to you is a normal 
  63. read/write socket, so you cannot accept() on it, even after the connection is 
  64. closed. Even if you could, it would be useless, since it is not bound to the 
  65. well-known port.
  66.  
  67.     -Kartik
  68.