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:Main.java

public static void main(String[] argv) throws Exception {
    int port = 80;

    Socket socket = new Socket("google.com", port, InetAddress.getByName("java2s.com"), port);

    socket.bind(new InetSocketAddress("java2s.com", port));
}

From source file:net.ruthandtodd.gpssync.rkoauth.RunkeeperOAuthClient.java

private static int getEphemeralPort() throws IOException {
    Socket s = new Socket();
    s.bind(null);
    try {//from   w ww.  ja va 2 s . com
        return s.getLocalPort();
    } finally {
        s.close();
    }
}

From source file:com.mirth.connect.connectors.tcp.SocketUtil.java

/**
 * Creates a socket and connects it to the specified remote host on the specified remote port.
 * /*from w ww .  j  a v a  2 s  .  co m*/
 * @param host
 *            - The remote host to connect on.
 * @param port
 *            - The remote port to connect on.
 * @param localAddr
 *            - The local address to bind the socket to.
 * @param localPort
 *            - The local port to bind the socket to.
 * @param timeout
 *            - The socket timeout to use when connecting.
 * @return The bound and connected Socket.
 * @throws UnknownHostException
 *             if the IP address of the host could not be determined
 * @throws IOException
 *             if an I/O error occurs when creating the socket
 */
public static Socket createSocket(TcpConfiguration configuration, String localAddr, int localPort)
        throws UnknownHostException, IOException {
    Socket socket = configuration.createSocket();

    if (StringUtils.isNotEmpty(localAddr)) {
        InetAddress localAddress = InetAddress.getByName(TcpUtil.getFixedHost(localAddr));
        socket.bind(new InetSocketAddress(localAddress, localPort));
    }

    return socket;
}

From source file:SocketFetcher.java

/**
 * Create a socket with the given local address and connected to
 * the given host and port.  Use the specified connection timeout.
 * If a socket factory is specified, use it.  Otherwise, use the
 * SSLSocketFactory if useSSL is true.//www  .j a v  a  2  s .co  m
 */
private static Socket createSocket(InetAddress localaddr, int localport, String host, int port, int cto,
        SocketFactory sf, boolean useSSL) throws IOException {
    Socket socket;

    if (sf != null)
        socket = sf.createSocket();
    else if (useSSL)
        socket = SSLSocketFactory.getDefault().createSocket();
    else
        socket = new Socket();
    if (localaddr != null)
        socket.bind(new InetSocketAddress(localaddr, localport));
    if (cto >= 0)
        socket.connect(new InetSocketAddress(host, port), cto);
    else
        socket.connect(new InetSocketAddress(host, port));
    return socket;
}

From source file:com.zimbra.common.net.ProtocolSocketFactoryWrapper.java

@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException {
    int timeout = params != null ? params.getConnectionTimeout() : 0;
    if (timeout > 0) {
        Socket sock = factory.createSocket();
        sock.bind(new InetSocketAddress(localAddress, localPort));
        sock.connect(new InetSocketAddress(host, port), timeout);
        return sock;
    } else {/*from   w w  w  .j av a  2 s  .  c o  m*/
        return factory.createSocket(host, port, localAddress, localPort);
    }
}

From source file:net.oauth.client.httpclient4.OAuthSchemeTest.java

@Override
public void setUp() throws Exception {
    { // Get an ephemeral local port number:
        Socket s = new Socket();
        s.bind(null);
        port = s.getLocalPort();/*from w  w w .  jav a2  s .c o m*/
        s.close();
    }
    server = new Server(port);
    Context servletContext = new Context(server, "/", Context.SESSIONS);
    servletContext.addServlet(new ServletHolder(new ProtectedResource()), "/Resource/*");
    server.start();
    context = new BasicHttpContext();
    context.setAttribute(ClientContext.AUTH_SCHEME_PREF, Arrays.asList(OAuthSchemeFactory.SCHEME_NAME));
    client = new DefaultHttpClient();
    client.getAuthSchemes().register(OAuthSchemeFactory.SCHEME_NAME, new OAuthSchemeFactory());
    client.getCredentialsProvider().setCredentials(new AuthScope("localhost", port),
            new OAuthCredentials(ProtectedResource.ACCESSOR));
}

From source file:com.cloud.network.bigswitch.TrustingProtocolSocketFactory.java

@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort,
        HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        return createSocket(host, port, localAddress, localPort);
    } else {//from www. ja  v  a2 s  .c om
        Socket s = ssf.createSocket();
        s.bind(new InetSocketAddress(localAddress, localPort));
        s.connect(new InetSocketAddress(host, port), timeout);
        return s;
    }
}

From source file:com.vmware.aurora.vc.vcservice.TlsSocketFactory.java

@Override
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");
    }/*  ww  w  .j  ava  2s . c om*/
    int timeout = params.getConnectionTimeout();
    SocketFactory socketfactory = getSSLContext().getSocketFactory();
    if (timeout == 0) {
        return socketfactory.createSocket(host, port, localAddress, localPort);
    } else {
        Socket socket = socketfactory.createSocket();
        socket.bind(new InetSocketAddress(localAddress, localPort));
        socket.connect(new InetSocketAddress(host, port), timeout);
        return socket;
    }
}

From source file:com.collabnet.svnedge.net.SslProtocolSocketFactory.java

public Socket createSocket(String remoteHost, int remotePort, InetAddress clientHost, int clientPort,
        HttpConnectionParams params) throws IOException, UnknownHostException, ConnectTimeoutException {
    if (params == null) {
        throw new IllegalArgumentException("Parameters may not be null");
    }/*from  ww w .j  av a2s  .c o  m*/

    int timeout = params.getConnectionTimeout();
    if (timeout == 0) {
        return getSocketFactory().createSocket(remoteHost, remotePort, clientHost, clientPort);
    } else {
        Socket socket = getSocketFactory().createSocket();
        socket.bind(new InetSocketAddress(clientHost, clientPort));
        socket.connect(new InetSocketAddress(remoteHost, remotePort), timeout);
        return socket;
    }
}

From source file:com.jivesoftware.os.jive.utils.http.client.CustomSecureProtocolSocketFactory.java

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

    Socket socket = sslSocketFactory.createSocket();

    if (localAddress != null && port > 0) {
        socket.bind(new InetSocketAddress(localAddress, localPort));
    }//  ww w  .  j  a v  a  2  s.  co  m

    int timeout = params.getSoTimeout();
    if (timeout > 0) {
        socket.setSoTimeout(timeout);
    }

    socket.connect(new InetSocketAddress(host, port), params.getConnectionTimeout());

    return socket;
}