public void test_getReuseAddress() throws Exception {
   ServerSocket theSocket = new ServerSocket();
   theSocket.setReuseAddress(true);
   assertTrue("getReuseAddress false when it should be true", theSocket.getReuseAddress());
   theSocket.setReuseAddress(false);
   assertFalse("getReuseAddress true when it should be False", theSocket.getReuseAddress());
 }
Exemple #2
0
  void ServerSocketTests() throws Exception {
    ServerSocket s1 = new ServerSocket();

    test("ServerSocket.setReuseAddress(true)");
    s1.setReuseAddress(true);
    check(s1.getReuseAddress());

    test("Socket.setReuseAddress(false)");
    s1.setReuseAddress(false);
    check(!s1.getReuseAddress());

    /* bind to any port */
    s1.bind(new InetSocketAddress(0));

    test("Binding ServerSocket to port already in use should throw " + "a BindException");
    ServerSocket s2 = new ServerSocket();
    try {
      s2.bind(new InetSocketAddress(s1.getLocalPort()));
      failed();
    } catch (BindException e) {
      passed();
    }
    s2.close();

    s1.close();
  }
  /** {@inheritDoc} */
  public Object getOption(SocketOption name) throws IOException {
    if (!(name instanceof StandardSocketOption)) {
      throw new IllegalArgumentException("Unsupported option " + name);
    }

    StandardSocketOption stdOpt = (StandardSocketOption) name;
    final ServerSocket socket = channel.socket();

    try {
      switch (stdOpt) {
        case SO_RCVBUF:
          return socket.getReceiveBufferSize();

        case SO_REUSEADDR:
          return socket.getReuseAddress();

        default:
          throw new IllegalArgumentException("Unsupported option " + name);
      }
    } catch (SocketException e) {
      if (socket.isClosed()) {
        throw Util.initCause(new ClosedChannelException(), e);
      }
      throw e;
    }
  }
 public boolean getReuseAddress() throws SocketException {
   return serverSocket.getReuseAddress();
 }