コード例 #1
0
ファイル: GCFSocketsImpl.java プロジェクト: johangas/moped
  /**
   * Opens a server TCP connection to clients. Creates, binds, and listens
   *
   * @param port local TCP port to listen on
   * @param backlog listen backlog.
   * @return a native handle to the network connection.
   * @throws IOException
   */
  public int openServer(int port, int backlog) throws IOException {
    int fd = -1;

    fd = sockets.socket(Socket.AF_INET, Socket.SOCK_STREAM, 0);
    if (DEBUG) {
      System.err.println("Socket.socket() = " + fd);
    }
    if (fd < 0) {
      throw newError(fd, "socket create");
    }

    set_blocking_flags(fd, /*is_blocking*/ false);

    setSockOpt(fd, Socket.SO_REUSEADDR, 1);

    setSockOpt(fd, Socket.IPPROTO_TCP, Socket.TCP_NODELAY, 1);

    Socket.sockaddr_in local_sin = new Socket.sockaddr_in();
    local_sin.sin_family = Socket.AF_INET;
    local_sin.sin_port = Inet.htons((short) port);
    local_sin.sin_addr = Socket.INADDR_ANY;
    if (DEBUG) {
      System.err.println("Socket.bind(" + fd + ", " + local_sin + ")");
    }

    if (sockets.bind(fd, local_sin, local_sin.size()) < 0) {
      throw newError(fd, "bind");
    }

    if (sockets.listen(fd, backlog) < 0) {
      throw newError(fd, "listen");
    }

    return fd;
  }
コード例 #2
0
ファイル: GCFSocketsImpl.java プロジェクト: johangas/moped
  /** @inheritDoc */
  public int open(String hostname, int port, int mode) throws IOException {
    // init_sockets(); win32 only
    int fd = -1;

    fd = sockets.socket(Socket.AF_INET, Socket.SOCK_STREAM, 0);
    if (DEBUG) {
      System.err.println("Socket.socket() = " + fd);
    }
    if (fd < 0) {
      throw newError(fd, "socket create");
    }

    set_blocking_flags(fd, /*is_blocking*/ false);

    NetDB.hostent phostent;
    // hostname is always NUL terminated. See socket/Protocol.java for detail.
    phostent = NetDB.INSTANCE.gethostbyname(hostname);
    if (phostent == null) {
      throw newError(fd, "gethostbyname (herrono = " + NetDB.INSTANCE.h_errno() + ")");
    }

    Socket.sockaddr_in destination_sin = new Socket.sockaddr_in();
    destination_sin.sin_family = Socket.AF_INET;
    destination_sin.sin_port = Inet.htons((short) port);
    destination_sin.sin_addr = phostent.h_addr_list[0];

    if (DEBUG) {
      System.err.println("Socket.sockaddr_in: " + destination_sin);
      System.err.println("connect: hostname: " + hostname + " port: " + port + " mode: " + mode);
    }
    if (sockets.connect(fd, destination_sin, destination_sin.size()) < 0) {
      int err_code = LibCUtil.errno();
      if (err_code == LibC.EINPROGRESS || err_code == LibC.EWOULDBLOCK) {
        // When the socket is ready for connect, it becomes *writable*
        // (according to BSD socket spec of select())
        VMThread.getSystemEvents().waitForWriteEvent(fd);

        int errno = getSockOpt(fd, Socket.SO_ERROR);
        if (errno != 0) {
          String msg;
          if (errno == LibC.ECONNREFUSED) {
            msg = "connect refused";
          } else {
            msg = String.valueOf(errno);
          }
          throw new IOException("ConnectException: " + msg);
        }

        if (DEBUG) {
          System.err.println("connect back fd = " + fd);
        }
      } else {
        throw newError(fd, "connect");
      }
    }

    try {
      setSockOpt(fd, Socket.IPPROTO_TCP, Socket.TCP_NODELAY, 1);
    } catch (IOException ex) {
    }

    return fd;
  }