Ejemplo n.º 1
0
  /**
   * Accept client connections on server socket fd. Blocks until a client connects.
   *
   * @param fd open server socket. See {@link #openServer}.
   * @return a native handle to the network connection.
   * @throws IOException
   */
  public int accept(int fd) throws IOException {
    Socket.sockaddr_in remote_sin = new Socket.sockaddr_in();
    IntByReference address_len = new IntByReference(4);
    int newSocket;

    try {
      if (DEBUG) {
        System.err.println("Socket.accept(" + fd + ", " + remote_sin + ")...");
      }

      newSocket = sockets.accept(fd, remote_sin, address_len);

      if (newSocket < 0) {
        if (DEBUG) {
          System.err.println("Need to block for accept...");
        }

        int err_code = LibCUtil.errno();
        if (err_code == LibC.EAGAIN || err_code == LibC.EWOULDBLOCK) {
          VMThread.getSystemEvents().waitForReadEvent(fd);
          newSocket = sockets.accept(fd, remote_sin, address_len);
          if (newSocket < 0) {
            throw newError(fd, "accept");
          }
        } else {
          // BUG!
          throw newError(fd, "accept");
        }
      }
    } finally {
      address_len.free();
    }

    set_blocking_flags(newSocket, /*is_blocking*/ false);
    // we could read info about client from remote_sin, but don't need to.

    if (DEBUG) {
      System.err.println("    Socket.accept(...) = " + newSocket);
    }
    return newSocket;
  }
Ejemplo n.º 2
0
  /**
   * Accept client connections on server socket fd. Blocks until a client connects.
   *
   * @param fd open server socket. See {@link #openServer}.
   * @return a native handle to the network connection.
   * @throws IOException
   */
  public int accept(int fd) throws IOException {
    VMThread.getSystemEvents().waitForReadEvent(fd);

    Socket.sockaddr_in remote_sin = new Socket.sockaddr_in();
    IntByReference address_len = new IntByReference(4);
    int newSocket = sockets.accept(fd, remote_sin, address_len);
    if (newSocket < 0) {
      throw newError(fd, "accept");
    }
    address_len.free();
    set_blocking_flags(newSocket, /*is_blocking*/ false);
    // we could read info about client from remote_sin, but don't need to.

    return newSocket;
  }