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:eu.stratosphere.nephele.taskmanager.TaskManager.java

public static boolean tryToConnect(InetAddress fromAddress, SocketAddress toSocket, int timeout)
        throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Trying to connect to JobManager (" + toSocket + ") from local address " + fromAddress
                + " with timeout " + timeout);
    }//from w w  w  .j a v  a2  s  .  c  o  m
    boolean connectable = true;
    Socket socket = null;
    try {
        socket = new Socket();
        SocketAddress bindP = new InetSocketAddress(fromAddress, 0); // 0 = let the OS choose the port on this
        // machine
        socket.bind(bindP);
        socket.connect(toSocket, timeout);
    } catch (Exception ex) {
        LOG.info("Failed to determine own IP address from '" + fromAddress + "': " + ex.getMessage());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Failed with exception", ex);
        }
        connectable = false;
    } finally {
        if (socket != null) {
            socket.close();
        }
    }
    return connectable;
}

From source file:org.eclipse.mylyn.internal.commons.http.PollingProtocolSocketFactory.java

public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {

    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null"); //$NON-NLS-1$
    }//from  w w  w. jav a  2  s. c  o  m

    final Socket socket = sock != null ? sock : factory.createSocket();

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);

    socket.bind(localAddress);
    MonitoredRequest.connect(socket, remoteAddress, connTimeout);
    return socket;
}

From source file:com.cazoodle.crawl.DummySSLProtocolSocketFactory.java

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");
    }//from  www .  j a v a  2 s . c  o m
    int timeout = params.getConnectionTimeout();
    if (timeout > 0) {
        Socket socket = getSSLContext().getSocketFactory().createSocket();
        socket.bind(new InetSocketAddress(localAddress, localPort));
        socket.setSoTimeout(timeout);
        socket.connect(new InetSocketAddress(getInetAddress(host), port), timeout);
        return socket;
    } else {
        return createSocket(host, port, localAddress, localPort);
    }
}

From source file:org.eclipse.mylyn.internal.commons.repositories.http.core.PollingProtocolSocketFactory.java

public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {

    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null"); //$NON-NLS-1$
    }//from  ww  w. j  ava2s  .  c  o m

    final Socket socket = sock != null ? sock : NetUtil.configureSocket(factory.createSocket());

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);

    socket.bind(localAddress);
    socket.connect(remoteAddress, connTimeout);
    return socket;
}

From source file:org.sonatype.nexus.apachehttpclient.NexusSSLConnectionSocketFactory.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 {
    checkNotNull(host);//from   w w  w. j  a v  a 2 s  .co m
    checkNotNull(remoteAddress);
    final Socket sock = socket != null ? socket : createSocket(context);
    if (localAddress != null) {
        sock.bind(localAddress);
    }
    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:com.serphacker.serposcope.scraper.http.extensions.ScrapClientPlainConnectionFactory.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, ConnectTimeoutException {

    Socket sock;
    if (socket != null) {
        sock = socket;//www .  ja  v  a  2s  . co m
    } else {
        sock = createSocket(context);
    }
    if (localAddress != null) {
        sock.bind(localAddress);
    }
    try {
        sock.connect(remoteAddress, connectTimeout);
    } catch (SocketTimeoutException ex) {
        throw new ConnectTimeoutException(ex, host, remoteAddress.getAddress());
    }
    return sock;
}

From source file:com.sun.faban.driver.transport.hc3.AboveTimedSSLSocketFactory.java

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort) throws IOException {

    Socket socket = new TimedSocketWrapper(sslFactory.createSocket());
    InetSocketAddress endpoint = new InetSocketAddress(host, port);
    socket.bind(new InetSocketAddress(localAddress, localPort));
    socket.connect(endpoint);/*from   w  ww.  j a v  a 2 s.  co  m*/
    return socket;
}

From source file:com.sun.faban.driver.transport.hc3.AboveTimedSSLSocketFactory.java

public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }/*from   www .  j a v  a 2s.  com*/
    int timeout = params.getConnectionTimeout();
    if (timeout <= 0) {
        return createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = new TimedSocketWrapper(sslFactory.createSocket());
        InetSocketAddress endpoint = new InetSocketAddress(host, port);
        socket.bind(new InetSocketAddress(localAddress, localPort));
        socket.connect(endpoint, timeout);
        return socket;
    }
}

From source file:greensopinion.restexample.test.web.WebApplicationContainer.java

private boolean testAvailablePort(int port) {
    try {//from  w  w  w. j  a  v a 2 s . c o  m
        Socket socket = SocketFactory.getDefault().createSocket();
        try {
            socket.bind(new InetSocketAddress("localhost", port));
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            socket.close();
        }
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.eclipse.mylyn.internal.commons.http.PollingSslProtocolSocketFactory.java

public Socket connectSocket(Socket sock, InetSocketAddress remoteAddress, InetSocketAddress localAddress,
        HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException {

    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null"); //$NON-NLS-1$
    }/*w  ww.  ja  v a2  s  . c  o m*/

    final Socket socket = getSocketFactory().createSocket();

    int connTimeout = HttpConnectionParams.getConnectionTimeout(params);

    socket.bind(localAddress);
    MonitoredRequest.connect(socket, remoteAddress, connTimeout);
    return socket;
}