Example usage for java.net Socket connect

List of usage examples for java.net Socket connect

Introduction

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

Prototype

public void connect(SocketAddress endpoint) throws IOException 

Source Link

Document

Connects this socket to the server.

Usage

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);
    return socket;
}

From source file:voldemort.store.socket.SocketPoolableObjectFactory.java

/**
 * Create a socket for the given host/port
 */// ww  w .  j a  v  a 2 s  .c om
public Object makeObject(Object key) throws Exception {
    SocketDestination dest = (SocketDestination) key;
    Socket socket = new Socket();
    socket.setReceiveBufferSize(this.socketBufferSize);
    socket.setSendBufferSize(this.socketBufferSize);
    socket.setTcpNoDelay(true);
    socket.setSoTimeout(soTimeoutMs);
    socket.connect(new InetSocketAddress(dest.getHost(), dest.getPort()));

    recordSocketCreation(dest, socket);

    return new SocketAndStreams(socket);
}

From source file:nl.nn.adapterframework.ftp.FTPsClient.java

protected Socket _openDataConnection_(int cmdNr, String param) throws IOException {
    // if explicit FTPS, the socket connection is establisch unsecure
    if (session.getFtpType() == FtpSession.FTPS_EXPLICIT_SSL
            || session.getFtpType() == FtpSession.FTPS_EXPLICIT_TLS) {
        if (session.isProtp()) {
            // With Prot P the result is returned over a different port
            // .. send protp commands  

            sendCommand("PBSZ", "0");
            checkReply("PBSZ 0");
            sendCommand("PROT", "P");
            checkReply("PROT P");
            sendCommand("PASV");
            checkReply("PASV");

            // Parse the host and port name to which the result is send
            String reply = getReplyString();
            String line = reply.substring(reply.indexOf('(') + 1, reply.lastIndexOf(')'));
            String[] hostinfo = line.split(",");
            String host = hostinfo[0] + "." + hostinfo[1] + "." + hostinfo[2] + "." + hostinfo[3];
            int port = (Integer.parseInt(hostinfo[4]) << 8) + Integer.parseInt(hostinfo[5]);
            log.debug("channel from pasv reply=" + host + ":" + port);
            InetSocketAddress address = new InetSocketAddress(host, port);

            // connect to the result address
            Socket socket = new Socket();
            socket.connect(address);
            socket.setSoTimeout(1000);// ww  w. j  av a  2 s.com
            host = socket.getInetAddress().getHostAddress();
            port = socket.getPort();
            log.debug("channel from socket=" + host + ":" + port);
            socket = socketFactory.createSocket(socket, host, port, true);

            String cmdLine = FTPCommand.getCommand(cmdNr);
            if (param != null) {
                cmdLine += ' ' + param;
            }
            // send the requested command (over the original socket)  <-- toch maar niet! GvB
            //            _sendCommand(cmdLine, _socket_.getOutputStream(), null);         
            sendCommand(cmdNr, param);

            // return the new socket for the reply 
            return socket;

        }
    }
    return super._openDataConnection_(cmdNr, param);
}

From source file:com.cloudant.tests.CloudantClientTests.java

/**
 * Check that the connection timeout throws a SocketTimeoutException when it can't connect
 * within the timeout.//w ww  .  j a  v  a  2  s  .com
 */
@Test(expected = SocketTimeoutException.class)
public void connectionTimeout() throws Throwable {

    ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0, 1);

    //block the single connection to our server
    Socket socket = new Socket();
    socket.connect(serverSocket.getLocalSocketAddress());

    //now try to connect, but should timeout because there is no connection available
    try {
        CloudantClient c = ClientBuilder.url(new URL("http://127.0.0.1:" + serverSocket.getLocalPort()))
                .connectTimeout(100, TimeUnit.MILLISECONDS).build();

        // Make a request
        c.getAllDbs();
    } catch (CouchDbException e) {
        //unwrap the CouchDbException
        if (e.getCause() != null) {
            //whilst it would be really nice to actually assert that this was a connect
            //exception and not some other SocketTimeoutException there are JVM differences in
            //this respect (i.e. OpenJDK does not appear to distinguish between read/connect)
            //in its exception messages
            throw e.getCause();
        } else {
            throw e;
        }
    } finally {
        //make sure we close the sockets
        IOUtils.closeQuietly(serverSocket);
        IOUtils.closeQuietly(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.//from  ww w .  ja  va  2s. 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.apache.hadoop.net.HopsSSLSocketFactory.java

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    Socket socket = createSocket();
    socket.connect(new InetSocketAddress(host, port));
    return socket;
}

From source file:org.apache.hadoop.net.HopsSSLSocketFactory.java

@Override
public Socket createSocket(InetAddress inetAddress, int port) throws IOException {
    Socket socket = createSocket();
    socket.connect(new InetSocketAddress(inetAddress, port));
    return socket;
}

From source file:org.apache.hadoop.net.HopsSSLSocketFactory.java

@Override
public Socket createSocket(String host, int port, InetAddress localAddress, int localPort)
        throws IOException, UnknownHostException {
    Socket socket = createSocket();
    socket.bind(new InetSocketAddress(localAddress, localPort));
    socket.connect(new InetSocketAddress(host, port));
    return socket;
}

From source file:org.apache.hadoop.net.HopsSSLSocketFactory.java

@Override
public Socket createSocket(InetAddress inetAddress, int port, InetAddress localAddress, int localPort)
        throws IOException {
    Socket socket = createSocket();
    socket.bind(new InetSocketAddress(localAddress, localPort));
    socket.connect(new InetSocketAddress(inetAddress, port));
    return socket;
}

From source file:org.kde.kdeconnect.Backends.LanBackend.LanLink.java

public void injectNetworkPackage(NetworkPackage np) {

    if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_ENCRYPTED)) {

        try {/*from w  ww . j  a  va2s .  co  m*/
            np = np.decrypt(privateKey);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("KDE/onPackageReceived", "Exception reading the key needed to decrypt the package");
        }

    }

    if (np.hasPayloadTransferInfo()) {

        Socket socket = null;
        try {
            socket = new Socket();
            int tcpPort = np.getPayloadTransferInfo().getInt("port");
            InetSocketAddress address = (InetSocketAddress) session.getRemoteAddress();
            socket.connect(new InetSocketAddress(address.getAddress(), tcpPort));
            np.setPayload(socket.getInputStream(), np.getPayloadSize());
        } catch (Exception e) {
            try {
                socket.close();
            } catch (Exception ignored) {
            }
            e.printStackTrace();
            Log.e("KDE/LanLink", "Exception connecting to payload remote socket");
        }

    }

    packageReceived(np);
}