From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96078608810129.tru64-unix-managers.marc@archive.invalid> From: Pedro Jose Serrahima Bartolome Date: Thu, 06 Feb 1997 15:54:41 +0000 To: tru64-unix-managers Subject: SUMMARY: How to clean up a CLOSE_WAIT socket X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96078608810129 Thanks to all osf managers who replied to my question, specially to Charles M. Richmond , who has dedicated to my problem more time than I could expect. These are the persons who replied: Charles M. Richmond John Hascall Arnaud Valeix Jeffrey G. Micono 6533 (Ktech) Peter Hudson My question was: ------ I'm programming a client/server application that sometimes crash abnormally, leaving one socket in CLOSE_WAIT state, as indicated by netstat command. My problem is not why the application crashed, but the fact that I don't know how to clean up this socket, and then, I cannot restart the program because the bind C function fails due to the error : 'address alrady in use' caused by the presence of this CLOSE_WAIT state socket. Up to this moment, the only way I've found to solve this is restarting the computer, but, of course, this is not a good solution. the O.S is Dunix 4.0 and 3.2. ---- Not considering the the cause of my program crash, that may be hard to find as Charles knows, John Hascall and Peter Hudson provided me some directions to avoid the problem caused to me when the program is crashed, allowing me to restart the application again without receiving the 'address already in use' error. These are their replies: >From John: -------------------------- Add a call to set the SO_REUSEADDR flag on the socket -- something like this: int on = 1; s = socket( ... ); setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); : bind(s, ... ); ------------------------ >From Peter: ------------------------- There is only one way to do this: you must close() the socket. I am in the same situation, and I had to install a signal handler for SIGTERM, SIGBUS, SIGSEGV, and so on... which closed the socket gracefully. Alternatively, in the code which opens/connects the socket, you could have a retry loop checking errno == EADDRINUSE. A socket in CLOSE_WAIT should timeout after about 2 minutes. ---------------------- Pedro Serrahima IIC Madrid Spain