/** @tests java.net.ServerSocket#getSoTimeout() */
 public void test_getSoTimeout() throws IOException {
   s = new ServerSocket(0);
   try {
     s.setSoTimeout(100);
     assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout());
   } finally {
     s.close();
   }
 }
  /** @tests java.net.ServerSocket#setSoTimeout(int) */
  public void test_setSoTimeoutI() throws IOException {
    // Timeout should trigger and throw InterruptedIOException
    try {
      s = new ServerSocket(0);
      s.setSoTimeout(100);
      s.accept();
    } catch (InterruptedIOException e) {
      assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout());
      return;
    }

    // Timeout should not trigger in this case
    s = new ServerSocket(0);
    startClient(s.getLocalPort());
    s.setSoTimeout(10000);
    sconn = s.accept();
  }
示例#3
0
  /**
   * Initializes this server's listener socket with the specified attributes, assuring that a socket
   * timeout has been set. The {@link #createServerSocket(int, int, InetAddress)} method can be
   * overridden to change the flavor of socket used.
   *
   * @see #createServerSocket(int, int, InetAddress)
   */
  private synchronized void setupServerSocket(int backlog) throws Exception {
    // Since we can't reliably set SO_REUSEADDR until JDK 1.4 is
    // the standard, try to (re-)open the server socket several
    // times.  Some OSes (Linux and Solaris, for example), hold on
    // to listener sockets for a brief period of time for security
    // reasons before relinquishing their hold.
    int attempt = 1;
    while (serverSocket == null) {
      try {
        serverSocket = createServerSocket(port, backlog, address);
      } catch (BindException e) {
        if (attempt == 10) {
          throw e;
        }

        attempt++;
        Thread.sleep(1000);
      }
    }

    if (XmlRpc.debug) {
      StringBuffer msg = new StringBuffer();
      msg.append("Opened XML-RPC server socket for ");
      msg.append(address != null ? address.getHostName() : "localhost");
      msg.append(':').append(port);
      if (attempt > 1) {
        msg.append(" after ").append(attempt).append(" tries");
      }
      System.out.println(msg.toString());
    }

    // A socket timeout must be set.
    if (serverSocket.getSoTimeout() <= 0) {
      serverSocket.setSoTimeout(4096);
    }
  }
 public int getSoTimeout() throws IOException {
   return serverSocket.getSoTimeout();
 }