Example #1
0
  void SocketTests() throws Exception {
    Socket s1 = new Socket();

    test("Socket should be created with SO_REUSEADDR disabled");
    check(!s1.getReuseAddress());

    test("Socket.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 Socket to port already in use should throw " + "a BindException");
    Socket s2 = new Socket();
    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 Socket socket = channel.socket();
    try {
      switch (stdOpt) {
        case SO_SNDBUF:
          return socket.getSendBufferSize();

        case SO_RCVBUF:
          return socket.getReceiveBufferSize();

        case SO_KEEPALIVE:
          return socket.getKeepAlive();

        case SO_REUSEADDR:
          return socket.getReuseAddress();

        case TCP_NODELAY:
          return socket.getTcpNoDelay();

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