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:org.eclipse.mylyn.internal.commons.net.PollingSslProtocolSocketFactory.java

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null"); //$NON-NLS-1$
    }// w w  w  .  j  a v  a2s . c o m

    int timeout = params.getConnectionTimeout();
    final Socket socket = NetUtil.configureSocket(getSocketFactory().createSocket());
    socket.bind(new InetSocketAddress(localAddress, localPort));
    MonitoredRequest.connect(socket, new InetSocketAddress(host, port), timeout);
    return socket;
}

From source file:org.elasticsearch.hadoop.rest.commonshttp.SocksSocketFactory.java

public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params)
        throws IOException, UnknownHostException, ConnectTimeoutException {

    InetSocketAddress socksAddr = new InetSocketAddress(socksHost, socksPort);
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksAddr);
    int timeout = params.getConnectionTimeout();

    Socket socket = new Socket(proxy);
    socket.setSoTimeout(timeout);/*from  w  w w  .  j a v  a  2 s .  co  m*/

    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.fabric3.binding.ftp.runtime.ExpiringSocketFactory.java

private Socket createSocket(InetSocketAddress socketAddress, InetSocketAddress localSocketAddress)
        throws IOException {
    Socket socket = new Socket();
    if (localSocketAddress != null) {
        socket.bind(localSocketAddress);
    }//from  w  ww  .  java  2s  . c  om
    socket.connect(socketAddress, connectTimeout);
    return socket;
}

From source file:org.glite.security.trustmanager.axis2.AXIS2SocketFactory.java

/**
 * Connect socket to remote host.//  ww  w .  jav a2s.c  o  m
 * 
 * @param socket the socket to be connected
 * @param remoteaddr remote host and port
 * @param localaddr optional local host and port
 * @param timeout optional timeout, default if used if timeout == 0
 * 
 * @return original socket
 **/
private final Socket connectSocket(Socket socket, SocketAddress remoteaddr, SocketAddress localaddr,
        int timeout) throws IOException {
    int newTimeout = timeout;
    if (localaddr != null) {
        socket.bind(localaddr);
    }

    // if no timeout is given, see if the property is set and use that
    if (timeout == 0) {
        String timeoutString = getCurrentProperties().getProperty(ContextWrapper.SSL_TIMEOUT_SETTING,
                ContextWrapper.TIMEOUT_DEFAULT);
        newTimeout = Integer.parseInt(timeoutString);
    }

    socket.setSoTimeout(newTimeout);
    socket.connect(remoteaddr, newTimeout);

    return socket;
}

From source file:org.imogene.client.ssl.EasySSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>/*from www .  j a va2 s.co 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
 */
@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"); //$NON-NLS-1$
    }
    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:org.jdesktop.http.SSLProtocolSocketFactory.java

/**
 * Attempts to get a new socket connection to the given host within the given time limit.
 * <p>//  w  w w.  jav  a2s . 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(host).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:org.lockss.util.urlconn.LockssDefaultProtocolSocketFactory.java

/**
 * This is the only factory method that handles params, thus the only one
 * we need to override./* w ww.j a  va  2 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 local port to bing the socket to
 * @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 if socket cannot be connected within
 * the given time limit
 */
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");
    }
    Socket sock = new Socket();
    sock.bind(new InetSocketAddress(localAddress, localPort));
    sock.setKeepAlive(params.getBooleanParameter(HttpClientUrlConnection.SO_KEEPALIVE, false));
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        sock.connect(new InetSocketAddress(host, port));
    } else {
        try {
            sock.connect(new InetSocketAddress(host, port), timeout);
        } catch (SocketTimeoutException e) {
            // Reproduce httpclient behavior - distinguish connect timeout from
            // data timeout
            String msg = "The host did not accept the connection within timeout of " + timeout + " ms";
            throw new ConnectTimeoutException(msg, e);
        }
    }
    return sock;
}

From source file:org.mule.transport.http.MuleSecureProtocolSocketFactory.java

/**
 * This is a direct version of code in {@link ReflectionSocketFactory}.
 */// w w w . j  a v  a 2s. com
protected Socket createSocketWithTimeout(String host, int port, InetAddress localAddress, int localPort,
        int timeout) throws IOException {
    Socket socket = socketFactory.createSocket();
    SocketAddress local = new InetSocketAddress(localAddress, localPort);
    SocketAddress remote = new InetSocketAddress(host, port);

    socket.bind(local);
    socket.connect(remote, timeout);
    return socket;
}

From source file:org.opensaml.ws.soap.client.http.TLSProtocolSocketFactory.java

/** {@inheritDoc} */
public Socket createSocket(String host, int port, InetAddress localHost, int localPort,
        HttpConnectionParams connParams) throws IOException {
    if (connParams == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }/*from   w ww.ja  v a 2s  .c  om*/
    int timeout = connParams.getConnectionTimeout();
    SocketFactory socketfactory = sslContext.getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localHost, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        SocketAddress localaddr = new InetSocketAddress(localHost, localPort);
        SocketAddress remoteaddr = new InetSocketAddress(host, port);
        socket.bind(localaddr);
        socket.connect(remoteaddr, timeout);
        return socket;
    }
}

From source file:org.oscarehr.olis.OLISProtocolSocketFactory.java

public Socket createSocket(final String host, final int port, final InetAddress localAddress,
        final int localPort, final HttpConnectionParams params) throws IOException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }/*from   ww w.j  ava2 s.c  om*/
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = context.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;
    }
}