home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / programm / 5227 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  3.1 KB

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!ames!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!bu.edu!cs!tasos
  2. From: tasos@cs.bu.edu (Anastasios Kotsikonas)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: SO_REUSEADDR (was: Re: Socket Programm
  5. Message-ID: <101253@bu.edu>
  6. Date: 10 Nov 92 01:30:16 GMT
  7. References: <101017@bu.edu> <1992Nov9.161326.19246@wuecl.wustl.edu>
  8. Sender: news@bu.edu
  9. Organization: Computer Science Department, Boston University, Boston, MA, USA
  10. Lines: 66
  11.  
  12. In article <1992Nov9.161326.19246@wuecl.wustl.edu> pete@arl.wustl.edu writes:
  13. >In article 101017@bu.edu, tasos@cs.bu.edu (Anastasios Kotsikonas) writes:
  14. >>In article <1992Nov7.023043.19007@wuecl.wustl.edu> pete@arl.wustl.edu writes:
  15. >   [.. my comment saying SO_REUSEADDR doesn't work as advertised...]
  16. >>
  17. >>I am sorry but this is nonsense. I have used SO_REUSEADDR on BSD 4.1.1, AIX
  18. >
  19. >hmmm, I think some more detail is needed...  I'm not saying your wrong
  20. >(please, no flames :-),  just that I've got conflicting stories...
  21.  
  22.  
  23. ... technical stuff [which I do not understand] deleted ...
  24.  
  25. Here is the code I am using that has NEVER had any problems:
  26.  
  27.  if ((sock = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
  28.     report_progress (report, "\ncreate_connection(): Could not create socket",
  29.              TRUE);
  30.     return -1;
  31.   }
  32.   if (setsockopt (sock, SOL_SOCKET, SO_SNDBUF, (char *) &sendbuf,
  33.           sizeof (sendbuf)) < 0)
  34.     report_progress (report, "\ncreate_connection(): Could not set socket \
  35. options", TRUE);
  36.   if (setsockopt (sock, SOL_SOCKET, SO_RCVBUF, (char *) &recvbuf,
  37.           sizeof (recvbuf)) < 0)
  38.     report_progress (report, "\ncreate_connection(): Could not set socket \
  39. options", TRUE);
  40.   if (setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, (char *) &val,
  41.       sizeof (val)) < 0)
  42.     report_progress (report, "\ncreate_connection():WARNING: Cannot toggle \
  43. keep-alive connections", TRUE);
  44.   val = 1;
  45.   if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char *) &val,
  46.       sizeof (val)) < 0)
  47.     report_progress (report, "\ncreate_connection():WARNING: Cannot toggle \
  48. reuse of local address", TRUE);
  49.  
  50. ... and now for the binf() ...:
  51.  
  52.   while (bind (sock, (struct sockaddr *) &server, sizeof (server)) < 0 &&
  53.      errno == EADDRINUSE && timeout < 180) { /* For perm port keep trying */
  54.     ++timeout;
  55.     errno = 0;
  56.     sleep (1);
  57.   }
  58.   if (timeout >= 180) {
  59.     report_progress (report, "\ncreate_connection(): Could not bind", TRUE);
  60.     return -1;
  61.   }
  62.  
  63.  
  64. TRUE, I expect bind to delay me, but it has never delayed more than 3 seconds
  65. (3 iterations of the loop).
  66.  
  67. >I really just want an answer to how to close a socket hard, in such a way that any data
  68. >still in the system is discarded, and I can start up right away again.  I realise that discarding
  69.  
  70. I use close() which guarrantees the fastest return to the program, and flushing
  71. of the data if the connection is broken, etc.
  72.  
  73. Lastly, I am very hesitant to believe that someone found a bug with
  74. SO_REUSEADDR after so many years in use! I would be convinced if I could read
  75. the code to inetd and in it such a problem was mentioned!!!
  76.  
  77. Tasos
  78.