List of usage examples for java.net Socket bind
public void bind(SocketAddress bindpoint) throws IOException
From source file:org.parosproxy.paros.network.SSLConnector.java
/** * Attempts to get a new socket connection to the given host within the * given time limit.// w w w. j ava2 s . c o m * * @param host * the host name/IP * @param port * the port on the host * @param localAddress * the local host name/IP to bind the socket to * @param localPort * the port on the local machine * @param params * {@link HttpConnectionParams Http connection parameters} * * @return Socket a new socket * * @throws IOException * if an I/O error occurs while creating the socket * @throws UnknownHostException * if the IP address of the host cannot be determined * @throws ConnectTimeoutException */ @Override public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); if (timeout == 0) { InetAddress hostAddress = getCachedMisconfiguredHost(host, port); if (hostAddress != null) { return clientSSLSockFactory.createSocket(hostAddress, port, localAddress, localPort); } try { SSLSocket sslSocket = (SSLSocket) clientSSLSockFactory.createSocket(host, port, localAddress, localPort); sslSocket.startHandshake(); return sslSocket; } catch (SSLException e) { if (!e.getMessage().contains(CONTENTS_UNRECOGNIZED_NAME_EXCEPTION)) { throw e; } hostAddress = InetAddress.getByName(host); cacheMisconfiguredHost(host, port, hostAddress); return clientSSLSockFactory.createSocket(hostAddress, port, localAddress, localPort); } } Socket socket = clientSSLSockFactory.createSocket(); SocketAddress localAddr = new InetSocketAddress(localAddress, localPort); socket.bind(localAddr); SocketAddress remoteAddr = new InetSocketAddress(host, port); socket.connect(remoteAddr, timeout); return socket; }
From source file:org.paxle.crawler.http.impl.AllSSLProtocolSocketFactory.java
/** * @see ProtocolSocketFactory#createSocket(String, int, InetAddress, int, HttpConnectionParams) *//* w w w .j a v a 2s .c om*/ public Socket createSocket(String host, int port, InetAddress localAddress, int localPort, HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) throw new NullPointerException("The connection-params must not be null"); int timeout = params.getConnectionTimeout(); if (timeout == 0) { return this.socketFactory.createSocket(host, port, localAddress, localPort); } else { Socket socket = this.socketFactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); return socket; } }
From source file:org.rifidi.emulator.io.comm.ip.tcpserver.TCPServerCommunicationTest.java
License:asdf
/** * Tests turning on the TCPServerCommunication while it is off. * /*from w w w . ja v a 2s . com*/ * */ public void testTurnOnWhenOff() { /* Turn on */ this.tcpComm.turnOn(); /* Make a client to connect to the server. */ Socket clientSocket = new Socket(); InetSocketAddress clientAddress = new InetSocketAddress("127.0.0.1", 0); /* Allow server socket to fully start */ synchronized (this) { try { this.wait(1000); } catch (InterruptedException e2) { /* Do nothing */ } this.notifyAll(); } /* Create a client to connect to the server */ try { /* Bind */ clientSocket.bind(clientAddress); try { /* Connect to the TCPServerCommunication. */ InetSocketAddress serverAddress = new InetSocketAddress(SERVER_HOSTNAME, SERVER_PORT); clientSocket.connect(serverAddress); } catch (IOException e) { /* Failed */ fail("Test client could not connect to Communication: " + e.getMessage()); } /* Allow server socket to fully start client services */ synchronized (this) { try { this.wait(1000); } catch (InterruptedException e2) { /* Do nothing */ } this.notifyAll(); } /* Close the socket. */ try { clientSocket.close(); } catch (IOException ioe) { /* Do nothing */ } } catch (IOException e) { fail("Cannot create client to test properly: " + e.getMessage()); } }
From source file:org.sonatype.nexus.internal.httpclient.NexusSSLConnectionSocketFactory.java
@Override @IgnoreJRERequirement/*w w w.j av a2 s .co m*/ public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { checkNotNull(host); checkNotNull(remoteAddress); final Socket sock = socket != null ? socket : createSocket(context); if (localAddress != null) { sock.bind(localAddress); } // NEXUS-6838: Server Name Indication support, a TLS feature that allows SSL // "virtual hosting" (multiple certificates) over single IP address + port. // Some CDN solutions requires this for HTTPS, as they choose certificate // to use based on "expected" hostname that is being passed here below // and is used during SSL handshake. Requires Java7+ if (sock instanceof SSLSocketImpl) { ((SSLSocketImpl) sock).setHost(host.getHostName()); } try { sock.connect(remoteAddress, connectTimeout); } catch (final IOException e) { Closeables.close(sock, true); throw e; } // Setup SSL layering if necessary if (sock instanceof SSLSocket) { final SSLSocket sslsock = (SSLSocket) sock; sslsock.startHandshake(); verifyHostname(sslsock, host.getHostName()); return sock; } else { return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context); } }
From source file:processing.app.debug.EasySSLProtocolSocketFactory.java
/** * Attempts to get a new socket connection to the given host within the given time limit. * <p>/*from www .j ava 2 s . c om*/ * To circumvent the limitations of older JREs that do not support connect timeout a * controller thread is executed. The controller thread attempts to create a new socket * within the given limit of time. If socket constructor does not return until the * timeout expires, the controller terminates and throws an {@link ConnectTimeoutException} * </p> * * @param host the host name/IP * @param port the port on the host * @param clientHost the local host name/IP to bind the socket to * @param clientPort the port on the local machine * @param params {@link HttpConnectionParams Http connection parameters} * @return Socket a new socket * @throws IOException if an I/O error occurs while creating the socket * @throws UnknownHostException if the IP address of the host cannot be * determined */ public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort, final HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } int timeout = params.getConnectionTimeout(); SocketFactory socketfactory = getSSLContext().getSocketFactory(); Socket socket; if (timeout == 0) { socket = socketfactory.createSocket(host, port, localAddress, localPort); } else { socket = socketfactory.createSocket(); SocketAddress localaddr = new InetSocketAddress(localAddress, localPort); SocketAddress remoteaddr = new InetSocketAddress(host, port); socket.bind(localaddr); socket.connect(remoteaddr, timeout); } SSLSocket sslSocket = (SSLSocket) socket; sslSocket.setEnabledProtocols(SSL_PROTOCOLS); sslSocket.setEnabledCipherSuites(SSL_CYPHER_SUITES); return socket; }