/** * Connects the socket with a remote address. A timeout of zero is interpreted as an infinite * timeout. The connection will then block until established or an error occurs. * * @param endpoint The address to connect to * @exception IOException If an error occurs * @exception IllegalArgumentException If the address type is not supported * @exception IllegalBlockingModeException If this socket has an associated channel, and the * channel is in non-blocking mode * @exception SocketTimeoutException If the timeout is reached * @since 1.4 */ public void connect(SocketAddress endpoint, int timeout) throws IOException { if (closed) throw new SocketException("Socket is closed"); if (!(endpoint instanceof InetSocketAddress)) throw new IllegalArgumentException("Address type not supported"); if (ch != null && !ch.isBlocking()) throw new IllegalBlockingModeException(); impl.connect(endpoint, timeout); }
/** * 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); }