Ejemplo n.º 1
0
  /**
   * Binds the socket to the givent local address/port
   *
   * @param bindpoint The address/port to bind to
   * @exception IOException If an error occurs
   * @exception SecurityException If a security manager exists and its checkConnect method doesn't
   *     allow the operation
   * @exception IllegalArgumentException If the address type is not supported
   * @since 1.4
   */
  public void bind(SocketAddress bindpoint) throws IOException {
    if (closed) throw new SocketException("Socket is closed");

    if (!(bindpoint instanceof InetSocketAddress)) throw new IllegalArgumentException();

    InetSocketAddress tmp = (InetSocketAddress) bindpoint;
    impl.bind(tmp.getAddress(), tmp.getPort());
  }
Ejemplo n.º 2
0
  /**
   * This constructor is where the real work takes place. Connect to the specified address and port.
   * Use default local values if not specified, otherwise use the local host and port passed in.
   * Create as stream or datagram based on "stream" argument.
   *
   * <p>
   *
   * @param raddr The remote address to connect to
   * @param rport The remote port to connect to
   * @param laddr The local address to connect to
   * @param lport The local port to connect to
   * @param stream true for a stream socket, false for a datagram socket
   * @exception IOException If an error occurs
   * @exception SecurityException If a security manager exists and its checkConnect method doesn't
   *     allow the operation
   */
  private Socket(InetAddress raddr, int rport, InetAddress laddr, int lport, boolean stream)
      throws IOException {
    this();
    this.inputShutdown = false;
    this.outputShutdown = false;

    if (impl == null) throw new IOException("Cannot initialize Socket implementation");

    SecurityManager sm = System.getSecurityManager();
    if (sm != null) sm.checkConnect(raddr.getHostName(), rport);

    impl.create(stream);

    // FIXME: JCL p. 1586 says if localPort is unspecified, bind to any port,
    // i.e. '0' and if localAddr is unspecified, use getLocalAddress() as
    // that default.  JDK 1.2 doc infers not to do a bind.
    if (laddr != null) impl.bind(laddr, lport);

    if (raddr != null) impl.connect(raddr, rport);
  }