Example usage for java.net Socket isConnected

List of usage examples for java.net Socket isConnected

Introduction

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

Prototype

public boolean isConnected() 

Source Link

Document

Returns the connection state of the socket.

Usage

From source file:com.linkedin.databus2.test.TestUtil.java

/**
 * Checks if a server is running on a given host and port
 * @param host        the server host/*w w w  . j  a v  a  2  s.  c o m*/
 * @param port        the server port
 * @param log         logger for diagnostic messages (can be null)
 * @return true if successful
 * @throws IOException
 */
public static boolean checkServerRunning(String host, int port, Logger log, boolean logError) {
    boolean success = false;

    try {
        Socket socket = new Socket(host, port);
        log.info("host=" + host + " port=" + port);
        log.info("Socket Info:" + socket.toString());
        log.info("IsConnected=" + socket.isConnected() + " isClosed=" + socket.isClosed() + " isBound="
                + socket.isBound());
        success = socket.isConnected();
        socket.close();
    } catch (ConnectException ce) {
        if (null != log)
            log.error("Fail to connect to port:" + port);
        if (logError && null != log)
            log.error("Connect error", ce);
        success = false;
    } catch (IOException e) {
        if (logError && null != log)
            log.error("connect error", e);
    } catch (RuntimeException e) {
        if (logError && null != log)
            log.error("runtime error", e);
    }

    return success;
}

From source file:com.impetus.client.cassandra.common.CassandraUtilities.java

/**
* 
* @param host//  ww w  . ja  v a  2  s.c  om
* @param port
* @return
*/
public static boolean verifyConnection(String host, int port) {
    Socket socket = null;
    try {
        socket = new Socket(host, port);
        socket.setReuseAddress(true);
        socket.setSoLinger(true, 0);
        boolean isConnected = socket.isConnected();
        return isConnected;
    } catch (UnknownHostException e) {
        logger.warn("{}:{} is still down", host, port);
        return false;
    } catch (IOException e) {
        logger.warn("{}:{} is still down", host, port);
        return false;
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {
            logger.warn("{}:{} is still down", host, port);
        }
    }
}

From source file:com.qmetry.qaf.automation.ui.UiDriverFactory.java

private static boolean isSeverRunning(String host, int port) {
    boolean isRunning = false;

    Socket socket = null;
    try {/*from ww  w  .j  av a 2 s .  c  o  m*/
        socket = new Socket(host, (port));
        isRunning = socket.isConnected();
    } catch (Exception exp) {
        logger.error("Error occured while checking Selenium : " + exp.getMessage());
    } finally {
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e) {

        }
    }
    return isRunning;
}

From source file:com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.distributed.google.GoogleProviderUtils.java

static private boolean checkIfProxyIsOpen(Proxy proxy) {
    boolean connected = false;
    int tries = 0;
    int port = proxy.getPort();

    while (!connected && tries < connectRetries) {
        tries++;/*www.j  a  va2  s.  co  m*/

        try {
            log.info("Attempting to connect to localhost:" + port + "...");
            Socket testSocket = new Socket("localhost", port);
            log.info("Connection opened");

            connected = testSocket.isConnected();
            testSocket.close();
        } catch (IOException e) {
            DaemonTaskHandler.safeSleep(TimeUnit.SECONDS.toMillis(5));
        }
    }

    return connected;
}

From source file:org.wso2.carbon.automation.extensions.servers.utils.ClientConnectionUtil.java

/**
 * Check whether the provided <code>port</code> is open
 *
 * @param port The port that needs to be checked
 * @return true if the <code>port</code> is open & false otherwise
 */// w  w  w  .jav  a2 s  . com
public static boolean isPortOpen(int port) {
    Socket socket = null;
    boolean isPortOpen = false;
    try {
        InetAddress address = InetAddress.getLocalHost();
        socket = new Socket(address, port);
        isPortOpen = socket.isConnected();
        if (isPortOpen) {
            log.info("Successfully connected to the server on port " + port);
        }
    } catch (IOException e) {
        log.info("Port " + port + " is Closed");
        isPortOpen = false;
    } finally {
        try {
            if ((socket != null) && (socket.isConnected())) {
                socket.close();
            }
        } catch (IOException e) {
            log.error("Can not close the socket with is used to check the server status ", e);
        }
    }
    return isPortOpen;
}

From source file:org.mule.transport.ldap.util.DSManager.java

public static boolean checkSocketNotConnected() {

    try {//w w  w  .j  a  va  2  s.co m
        final Socket s = new Socket("localhost", 10389);

        if (s.isConnected()) {
            logger.debug("client socket is connected (server socket bound)");
        }
        s.close();
        return false;
    } catch (final Exception e) {

        logger.debug("client Socket not connected " + e.toString());

    }

    try {
        final ServerSocket s = new ServerSocket(10389);
        if (s.isBound()) {
            logger.debug("server socket is bound (=was therefore free)");
        }
        s.close();
        return true;
    } catch (final Exception e) {
        logger.debug("Server socket already bound " + e.toString());
        // e.printStackTrace();
        return false;
    }

}

From source file:gridool.util.net.SocketUtils.java

/**
 * @param connectTimeout A timeout of zero is interpreted as an infinite timeout.
 * @param pollDelay sleep in mills before retry.
 * @param maxRetry No-retry if the value is 1.
 *///  w ww. java  2  s.  c  o  m
public static Socket openSocket(Socket socket, SocketAddress sockAddr, int connectTimeout, long pollDelay,
        int maxRetry) throws IOException {
    assert (sockAddr != null);
    assert (pollDelay >= 0) : pollDelay;
    assert (maxRetry > 0) : maxRetry;
    for (int i = 0; i < maxRetry; i++) {
        try {
            socket.connect(sockAddr, connectTimeout);
        } catch (SocketTimeoutException ste) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Socket.connect to " + sockAddr + " timeout #" + (i + 1));
            }
        } catch (IOException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Socket.connect to " + sockAddr + " failed #" + (i + 1), e);
            }
        } catch (Throwable e) {
            LOG.fatal("failed to connect: " + sockAddr, e);
            throw new IOException(e);
        }
        if (socket.isConnected()) {
            return socket;
        }
        if (pollDelay > 0) {
            try {
                Thread.sleep(pollDelay);
            } catch (InterruptedException ie) {
                ;
            }
        }
    }
    throw new InterruptedIOException("Could not connect to " + sockAddr);
}

From source file:com.smartwork.client.gsocket.CommonSocketFactory.java

public boolean validateObject(Object key, Object obj) {
    Socket socket = (Socket) obj;
    return socket.isConnected();
}

From source file:org.workin.fastdfs.factory.DefaultPoolableTrackerServerFactory.java

@Override
public void activateObject(TrackerServer trackerServer) throws Exception {
    Socket socket = trackerServer.getSocket();
    if (!socket.isConnected() || socket.isClosed()) {
        /* ?TrackerServer */
        socket.setSoTimeout(ClientGlobal.g_network_timeout);
        socket.connect(trackerServer.getInetSocketAddress(), ClientGlobal.g_connect_timeout);
    }//from   ww w  . ja v a2 s  .c  o m
}

From source file:org.workin.fastdfs.factory.DefaultPoolableTrackerServerFactory.java

@Override
public boolean validateObject(TrackerServer trackerServer) {
    try {// w ww . j  a v  a2  s.  c om
        Socket socket = trackerServer.getSocket();
        if (!socket.isConnected() || socket.isClosed())
            return false;
    } catch (IOException e) {
        return false;
    }
    return true;
}