Example usage for java.nio.channels SocketChannel open

List of usage examples for java.nio.channels SocketChannel open

Introduction

In this page you can find the example usage for java.nio.channels SocketChannel open.

Prototype

public static SocketChannel open() throws IOException 

Source Link

Document

Opens a socket channel.

Usage

From source file:edu.hawaii.soest.kilonalu.ctd.CTDSource.java

/**
* A method used to the TCP socket of the remote source host for communication
* @param host       the name or IP address of the host to connect to for the
*                   socket connection (reading)
* @param portNumber the number of the TCP port to connect to (i.e. 2604)
*//*from   w ww  . j  a v a  2 s.c o  m*/
protected SocketChannel getSocketConnection() {

    String host = getHostName();
    int portNumber = new Integer(getHostPort()).intValue();
    SocketChannel dataSocket = null;

    try {

        // create the socket channel connection to the data source via the 
        // converter serial2IP converter      
        dataSocket = SocketChannel.open();
        dataSocket.connect(new InetSocketAddress(host, portNumber));

        // if the connection to the source fails, also disconnect from the RBNB
        // server and return null
        if (!dataSocket.isConnected()) {
            dataSocket.close();
            disconnect();
            dataSocket = null;
        }
    } catch (UnknownHostException ukhe) {

        logger.info("Unable to look up host: " + host + "\n");
        disconnect();
        dataSocket = null;
    } catch (IOException nioe) {
        logger.info("Couldn't get I/O connection to: " + host);
        disconnect();
        dataSocket = null;
    } catch (Exception e) {
        disconnect();
        dataSocket = null;
    }
    return dataSocket;

}

From source file:com.cloud.hypervisor.hyperv.resource.HypervDirectConnectResource.java

protected String connect(final String vmName, final String ipAddress, final int port) {
    final long startTick = System.currentTimeMillis();

    // wait until we have at least been waiting for _ops_timeout time or
    // at least have tried _retry times, this is to coordinate with system
    // VM patching/rebooting time that may need
    int retry = _retry;
    while (System.currentTimeMillis() - startTick <= _opsTimeout || --retry > 0) {
        s_logger.info("Trying to connect to " + ipAddress);
        try (SocketChannel sch = SocketChannel.open();) {
            sch.configureBlocking(true);
            sch.socket().setSoTimeout(5000);
            // we need to connect to the control ip address to check the status of the system vm
            final InetSocketAddress addr = new InetSocketAddress(ipAddress, port);
            sch.connect(addr);// w w  w . jav a  2  s  .  c  o m
            return null;
        } catch (final IOException e) {
            s_logger.info("Could] not connect to " + ipAddress + " due to " + e.toString());
            if (e instanceof ConnectException) {
                // if connection is refused because of VM is being started,
                // we give it more sleep time
                // to avoid running out of retry quota too quickly
                try {
                    Thread.sleep(5000);
                } catch (final InterruptedException ex) {
                    s_logger.debug(
                            "[ignored] interupted while waiting to retry connecting to vm after exception: "
                                    + e.getLocalizedMessage());
                }
            }
        }

        try {
            Thread.sleep(1000);
        } catch (final InterruptedException ex) {
            s_logger.debug("[ignored] interupted while connecting to vm.");
        }
    }

    s_logger.info("Unable to logon to " + ipAddress);

    return "Unable to connect";
}

From source file:com.cloud.hypervisor.vmware.resource.VmwareResource.java

protected String connect(final String vmName, final String ipAddress, final int port) {
    long startTick = System.currentTimeMillis();

    // wait until we have at least been waiting for _ops_timeout time or
    // at least have tried _retry times, this is to coordinate with system
    // VM patching/rebooting time that may need
    int retry = _retry;
    while (System.currentTimeMillis() - startTick <= _opsTimeout || --retry > 0) {
        s_logger.info("Trying to connect to " + ipAddress);
        try (SocketChannel sch = SocketChannel.open();) {
            sch.configureBlocking(true);
            sch.socket().setSoTimeout(5000);

            InetSocketAddress addr = new InetSocketAddress(ipAddress, port);
            sch.connect(addr);/*from   w  w w  .  j a va 2s  .  c  om*/
            return null;
        } catch (IOException e) {
            s_logger.info("Could not connect to " + ipAddress + " due to " + e.toString());
            if (e instanceof ConnectException) {
                // if connection is refused because of VM is being started,
                // we give it more sleep time
                // to avoid running out of retry quota too quickly
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    s_logger.debug("[ignored] interupted while waiting to retry connect after failure.", e);
                }
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException ex) {
            s_logger.debug("[ignored] interupted while waiting to retry connect.");
        }
    }

    s_logger.info("Unable to logon to " + ipAddress);

    return "Unable to connect";
}