diff options
Diffstat (limited to 'java/src')
-rw-r--r-- | java/src/IceInternal/Network.java | 16 | ||||
-rw-r--r-- | java/src/IceInternal/TcpAcceptor.java | 17 | ||||
-rw-r--r-- | java/src/IceInternal/UdpTransceiver.java | 17 |
3 files changed, 50 insertions, 0 deletions
diff --git a/java/src/IceInternal/Network.java b/java/src/IceInternal/Network.java index 4f01e678c54..f4fafc7ba17 100644 --- a/java/src/IceInternal/Network.java +++ b/java/src/IceInternal/Network.java @@ -222,6 +222,22 @@ public final class Network } } + public static void + setReuseAddress(java.nio.channels.ServerSocketChannel fd, boolean reuse) + { + try + { + fd.socket().setReuseAddress(reuse); + } + catch(java.io.IOException ex) + { + closeSocketNoThrow(fd); + Ice.SocketException se = new Ice.SocketException(); + se.initCause(ex); + throw se; + } + } + public static java.net.InetSocketAddress doBind(java.nio.channels.ServerSocketChannel fd, java.net.InetSocketAddress addr) { diff --git a/java/src/IceInternal/TcpAcceptor.java b/java/src/IceInternal/TcpAcceptor.java index e69ce047025..c2b7e0751e5 100644 --- a/java/src/IceInternal/TcpAcceptor.java +++ b/java/src/IceInternal/TcpAcceptor.java @@ -208,6 +208,23 @@ class TcpAcceptor implements Acceptor _fd = Network.createTcpServerSocket(); Network.setBlock(_fd, false); Network.setTcpBufSize(_fd, _instance.initializationData().properties, _logger); + if(!System.getProperty("os.name").startsWith("Windows")) + { + // + // Enable SO_REUSEADDR on Unix platforms to allow + // re-using the socket even if it's in the TIME_WAIT + // state. On Windows, this doesn't appear to be + // necessary and enabling SO_REUSEADDR would actually + // not be a good thing since it allows a second + // process to bind to an address even it's already + // bound by another process. + // + // TODO: using SO_EXCLUSIVEADDRUSE on Windows would + // probably be better but it's only supported by recent + // Windows versions (XP SP2, Windows Server 2003). + // + Network.setReuseAddress(_fd, true); + } _addr = new java.net.InetSocketAddress(host, port); if(_traceLevels.network >= 2) { diff --git a/java/src/IceInternal/UdpTransceiver.java b/java/src/IceInternal/UdpTransceiver.java index c2c3637ed56..2fb9a63e735 100644 --- a/java/src/IceInternal/UdpTransceiver.java +++ b/java/src/IceInternal/UdpTransceiver.java @@ -395,6 +395,23 @@ final class UdpTransceiver implements Transceiver } else { + if(!System.getProperty("os.name").startsWith("Windows")) + { + // + // Enable SO_REUSEADDR on Unix platforms to allow + // re-using the socket even if it's in the TIME_WAIT + // state. On Windows, this doesn't appear to be + // necessary and enabling SO_REUSEADDR would actually + // not be a good thing since it allows a second + // process to bind to an address even it's already + // bound by another process. + // + // TODO: using SO_EXCLUSIVEADDRUSE on Windows would + // probably be better but it's only supported by recent + // Windows versions (XP SP2, Windows Server 2003). + // + Network.setReuseAddress(_fd, true); + } _addr = Network.doBind(_fd, _addr); } |