@Override public boolean isReuseAddress() { try { return socket.getReuseAddress(); } catch (SocketException e) { throw new ChannelException(e); } }
void DatagramSocketTests() throws Exception { DatagramSocket s1 = new DatagramSocket(null); test("DatagramSocket should be created with SO_REUSEADDR disabled"); check(!s1.getReuseAddress()); test("DatagramSocket.setReuseAddress(true)"); s1.setReuseAddress(true); check(s1.getReuseAddress()); test("DatagramSocket.setReuseAddress(false)"); s1.setReuseAddress(false); check(!s1.getReuseAddress()); /* bind to any port */ s1.bind(new InetSocketAddress(0)); test("Binding datagram socket to port already in use should throw " + "a BindException"); DatagramSocket s2 = new DatagramSocket(null); try { s2.bind(new InetSocketAddress(s1.getLocalPort())); failed(); } catch (BindException e) { passed(); } s2.close(); s1.close(); // bind with SO_REUSEADDR enabled s1 = new DatagramSocket(null); s1.setReuseAddress(true); s1.bind(new InetSocketAddress(0)); test( "Bind 2 datagram sockets to the same port - second " + "bind doesn't have SO_REUSEADDR enabled"); s2 = new DatagramSocket(null); try { s2.bind(new InetSocketAddress(s1.getLocalPort())); failed(); } catch (BindException e) { passed(); } s2.close(); test("Bind 2 datagram sockets to the same port - both have " + "SO_REUSEADDR enabled"); s2 = new DatagramSocket(null); s2.setReuseAddress(true); try { s2.bind(new InetSocketAddress(s1.getLocalPort())); passed(); } catch (BindException e) { if (System.getProperty("sun.net.useExclusiveBind") != null) { // exclusive bind enabled - expected result passed(); } else { failed(); } } s2.close(); s1.close(); }