Example usage for java.net Socket bind

List of usage examples for java.net Socket bind

Introduction

In this page you can find the example usage for java.net Socket bind.

Prototype

public void bind(SocketAddress bindpoint) throws IOException 

Source Link

Document

Binds the socket to a local address.

Usage

From source file:AuthSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>/*from w ww  .ja  v a 2  s. c  o  m*/
 * 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 params {@link HttpConnectionParams Http connection parameters}
 * @return Socket a new socket
 * @throws java.io.IOException           if an I/O error occurs while creating the socket
 * @throws java.net.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();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = 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:com.qspin.qtaste.tcom.rlogin.RLogin.java

/**
 * Reboot the remote host by sending the reboot command and check that
 * the remote host is not accessible anymore.
 * @return true if success, false otherwise
 *//*from   www .j  a va  2  s. co  m*/
public boolean reboot() {
    if (!sendCommand("reboot")) {
        return false;
    }

    // wait 1 second
    try {
        Thread.sleep(1000);
    } catch (InterruptedException ex) {
    }

    disconnect();

    // check that remote host is not accessible anymore
    // open a socket without any parameters. It hasn't been binded or connected
    Socket socket = new Socket();
    try {
        // bind to a local ephemeral port
        socket.bind(null);
        socket.connect(new InetSocketAddress(remoteHost, RLoginClient.DEFAULT_PORT), 1);
    } catch (SocketTimeoutException e) {
        logger.info("Rebooted host " + remoteHost + " successfully");
        return true;
    } catch (IOException e) {
        logger.error("Something went wrong while rebooting host:" + remoteHost);
        return false;
    } finally {
        try {
            socket.close();
        } catch (IOException ex) {
        }
        socket = null;
    }
    // Expected to get an exception as the remote host should not be reachable anymore
    logger.error("Host " + remoteHost
            + " did not reboot as expected! Please check that no other rlogin client is connected!");
    return false;
}

From source file:com.owncloud.android.network.AdvancedSslSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the
 * given time limit.//from  ww w.  j a  v a 2  s. com
 * 
 * @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 {
    Log_OC.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":"
            + localPort + ", params: " + params);
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log_OC.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
}

From source file:com.jaspersoft.ireport.jasperserver.ws.IReportSSLSocketFactory.java

/**
        //from w  w w  . j a v a 2 s  .  com
 * Attempts to get a new socket connection to the given host within the given time limit.
        
 * <p>
        
 * 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();

    if (timeout == 0) {

        return socketfactory.createSocket(host, port, localAddress, localPort);

    } else {

        Socket socket = 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:com.owncloud.android.oc_framework.network.AdvancedSslSocketFactory.java

/**
  * Attempts to get a new socket connection to the given host within the
  * given time limit./*  w w  w. j a v a 2  s  . c om*/
  * 
  * @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 {
    Log.d(TAG, "Creating SSL Socket with remote " + host + ":" + port + ", local " + localAddress + ":"
            + localPort + ", params: " + params);
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = params.getConnectionTimeout();

    //logSslInfo();

    SocketFactory socketfactory = mSslContext.getSocketFactory();
    Log.d(TAG, " ... with connection timeout " + timeout + " and socket timeout " + params.getSoTimeout());
    Socket socket = socketfactory.createSocket();
    SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
    SocketAddress remoteaddr = new InetSocketAddress(host, port);
    socket.setSoTimeout(params.getSoTimeout());
    socket.bind(localaddr);
    ServerNameIndicator.setServerNameIndication(host, (SSLSocket) socket);
    socket.connect(remoteaddr, timeout);
    verifyPeerIdentity(host, port, socket);
    return socket;
}

From source file:org.pluroid.pluroium.HttpClientFactory.java

public Socket connectSocket(Socket socket, String host, int port, InetAddress localAddress, int localPort,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (socket == null) {
        socket = createSocket();//from ww w.  java  2  s .c o  m
    }
    if ((localAddress != null) || (localPort > 0)) {
        if (localPort < 0) {
            localPort = 0; // indicates "any"
        }
        socket.bind(new InetSocketAddress(localAddress, localPort));
    }
    socket.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
    socket.connect(new InetSocketAddress(host, port), HttpConnectionParams.getConnectionTimeout(params));
    return socket;
}

From source file:org.apache.solr.client.solrj.impl.BasicHttpSolrServerTest.java

private int findUnusedPort() {
    for (int port = 0; port < 65535; port++) {
        Socket s = new Socket();
        try {/*from   ww  w  .  ja  v  a  2s  .  c  o  m*/
            s.bind(null);
            int availablePort = s.getLocalPort();
            s.close();
            return availablePort;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    throw new RuntimeException("Could not find unused TCP port.");
}

From source file:org.apache.commons.httpclient.contrib.ssl.AuthSSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>//from  ww  w.  jav  a 2 s.  c o  m
 * 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 HttpParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }
    int timeout = HttpConnectionParams.getConnectionTimeout(params);
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = 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:info.guardianproject.netcipher.client.SSLConnectionSocketFactory.java

@Override
public Socket connectSocket(final int connectTimeout, final Socket socket, final HttpHost host,
        final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context)
        throws IOException {
    Args.notNull(host, "HTTP host");
    Args.notNull(remoteAddress, "Remote address");
    final Socket sock = socket != null ? socket : createSocket(context);
    if (localAddress != null) {
        sock.bind(localAddress);
    }/*  w  w  w  .j av  a  2  s . co  m*/
    try {
        if (connectTimeout > 0 && sock.getSoTimeout() == 0) {
            sock.setSoTimeout(connectTimeout);
        }
        /*
              if (this.log.isDebugEnabled()) {
                this.log.debug("Connecting socket to " + remoteAddress + " with timeout " + connectTimeout);
              }
        */
        sock.connect(remoteAddress, connectTimeout);
    } catch (final IOException ex) {
        try {
            sock.close();
        } catch (final IOException ignore) {
        }
        throw ex;
    }
    // Setup SSL layering if necessary
    if (sock instanceof SSLSocket) {
        final SSLSocket sslsock = (SSLSocket) sock;
        //      this.log.debug("Starting handshake");
        sslsock.startHandshake();
        verifyHostname(sslsock, host.getHostName());
        return sock;
    } else {
        return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context);
    }
}

From source file:au.edu.monash.merc.capture.util.httpclient.ssl.SocketFactoryWrapper.java

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    // Based on code from EasySSLProtocolSocketFactory.java
    Socket rval;
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }//from  w w w  . j av a  2s  . c  om
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        rval = socketFactory.createSocket(host, port, localAddress, localPort);
    } else {
        rval = socketFactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localAddress, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        rval.bind(localaddr);
        rval.connect(remoteaddr, timeout);
    }
    return rval;
}