From 07e71cbac36847634a0ab67cfc7fb77228e18f8c Mon Sep 17 00:00:00 2001 From: rfm Date: Tue, 21 Nov 2006 15:11:56 +0000 Subject: [PATCH] Fix bug with server sockets. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@24144 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 7 +++++++ Source/unix/NSStream.m | 21 ++++++++++++++++++--- Source/win32/NSStreamWin32.m | 18 +++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2f5f0ab69..c468b0561 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-11-21 Richard Frith-Macdonald + + * unix/NSStream.m: + * win32/NSStreamWin32.m: + Re-use socket ports so that programs can start up again immediately + after they have shut down. + 2006-11-20 Richard Frith-Macdonald * Source/NSCalendarDate.m: Fix to parse timezone names as being diff --git a/Source/unix/NSStream.m b/Source/unix/NSStream.m index 669db8609..f289d173e 100644 --- a/Source/unix/NSStream.m +++ b/Source/unix/NSStream.m @@ -1429,10 +1429,25 @@ static void setNonblocking(int fd) - (void) open { - int bindReturn = bind((int)(intptr_t)_loopID, [self serverAddr], [self sockLen]); - int listenReturn = listen((intptr_t)_loopID, SOCKET_BACKLOG); + int bindReturn; + int listenReturn; - if (bindReturn < 0 || listenReturn) +#ifndef BROKEN_SO_REUSEADDR + /* + * Under decent systems, SO_REUSEADDR means that the port can be reused + * immediately that this process exits. Under some it means + * that multiple processes can serve the same port simultaneously. + * We don't want that broken behavior! + */ + int status = 1; + + setsockopt((int)(intptr_t)_loopID, SOL_SOCKET, SO_REUSEADDR, + (char *)&status, sizeof(status)); +#endif + + bindReturn = bind((int)(intptr_t)_loopID, [self serverAddr], [self sockLen]); + listenReturn = listen((intptr_t)_loopID, SOCKET_BACKLOG); + if (bindReturn < 0 || listenReturn < 0) { [self _recordError]; return; diff --git a/Source/win32/NSStreamWin32.m b/Source/win32/NSStreamWin32.m index 6e4aefeea..5ae685ef9 100644 --- a/Source/win32/NSStreamWin32.m +++ b/Source/win32/NSStreamWin32.m @@ -2213,10 +2213,22 @@ done: - (void) open { - int bindReturn = bind(_sock, [self serverAddr], [self sockLen]); - int listenReturn = listen(_sock, SOCKET_BACKLOG); + int bindReturn; + int listenReturn; +#ifndef BROKEN_SO_REUSEADDR + int status = 1; - if (bindReturn < 0 || listenReturn) + /* + * Under decent systems, SO_REUSEADDR means that the port can be reused + * immediately that this process exits. Under some it means + * that multiple processes can serve the same port simultaneously. + * We don't want that broken behavior! + */ + setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&status, sizeof(status)); +#endif + bindReturn = bind(_sock, [self serverAddr], [self sockLen]); + listenReturn = listen(_sock, SOCKET_BACKLOG); + if (bindReturn < 0 || listenReturn < 0) { [self _recordError]; return;