Example usage for java.net Socket close

List of usage examples for java.net Socket close

Introduction

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

Prototype

public synchronized void close() throws IOException 

Source Link

Document

Closes this socket.

Usage

From source file:hu.netmind.beankeeper.node.impl.NodeManagerImpl.java

/**
 * Determine if an address is available.
 *//* www  .j ava 2 s. c  o m*/
public static boolean isAlive(String ips, int port) {
    if (logger.isDebugEnabled())
        logger.debug("trying to reach: " + ips + ":" + port);
    try {
        if ("".equals(ips))
            ips = InetAddress.getLocalHost().getHostAddress();
    } catch (Exception e) {
        throw new StoreException(
                "can not determine local adapter, but there is another node, which would need to be contacted.",
                e);
    }
    StringTokenizer tokens = new StringTokenizer(ips, ",");
    while (tokens.hasMoreTokens()) {
        String ip = tokens.nextToken();
        if (logger.isDebugEnabled())
            logger.debug("determining whether '" + ip + ":" + port + "' is alive...");
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(ip, port), SOCKET_CONNECT_TIMEOUT);
            socket.close();
            return true; // Success, so return true
        } catch (Exception e) {
            logger.debug("unreachable node at '" + ip + ":" + port + ", " + e.getMessage());
        }
    }
    logger.debug("could not reach any of the ips given for node");
    return false;
}

From source file:com.hazelcast.stabilizer.Utils.java

public static void closeQuietly(Socket socket) {
    if (socket == null) {
        return;//from   w  w  w . ja v  a2  s. co m
    }

    try {
        socket.close();
    } catch (IOException ignore) {
    }
}

From source file:com.depas.utils.FileUtils.java

/**
 * Close socket if not null/*from   www  . j  a v  a 2 s  . com*/
 * @param socket is given socket
 */
public static void closeSocket(Socket socket) {
    if (socket != null) {
        try {
            socket.close();
        } catch (IOException e) {
            logger.warn("Unable to close the socket: " + e, e);
        }
    }
}

From source file:com.huawei.streaming.cql.LocalTaskCommons.java

private static void waitingUtilClose() {
    while (true) {
        Socket socket = null;
        try {/*w  ww  .ja va  2 s .  c  om*/
            socket = new Socket("127.0.0.1", 7999);
            socket.setKeepAlive(true);
            LOG.info("port is open now, waiting one second to retry");
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            break;
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

From source file:com.alibaba.jstorm.yarn.utils.JstormYarnUtils.java

/**
 * See if a port is available for listening on by trying connect to it
 * and seeing if that works or fails/* w  w w . ja va 2 s.c  o  m*/
 *
 * @param host
 * @param port
 * @return
 */
public static boolean isPortAvailable(String host, int port) {
    try {
        Socket socket = new Socket(host, port);
        socket.close();
        return false;
    } catch (IOException e) {
        return true;
    }
}

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 {//ww w  .  j  a  v  a  2  s  . co  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.hazelcast.stabilizer.Utils.java

public static String getHostAddress() {
    if (hostAddress != null) {
        return hostAddress;
    }/*w  w  w .  java  2 s  . c  om*/

    synchronized (Utils.class) {
        try {
            if (hostAddress != null) {
                return hostAddress;
            }
            Socket s = new Socket("google.com", 80);
            hostAddress = s.getLocalAddress().getHostAddress();
            s.close();
            return hostAddress;
        } catch (IOException io) {
            throw new RuntimeException(io);
        }
    }
}

From source file:Proxy.java

static void close(Socket sock) {
    if (sock != null) {
        try {//from  w w w.  java  2  s  . co m
            sock.close();
        } catch (Exception ex) {
        }
    }
}

From source file:com.zhch.example.commons.http.v4_5.ProxyTunnelDemo.java

/**
 * ?? demo// w w  w  . j  av  a2  s  .c o m
 * 
 * @throws Exception
 */
public final static void officalDemo() throws Exception {
    ProxyClient proxyClient = new ProxyClient();
    HttpHost target = new HttpHost("www.yahoo.com", 80);
    HttpHost proxy = new HttpHost("localhost", 8888);
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user", "pwd");
    Socket socket = proxyClient.tunnel(proxy, target, credentials);
    try {
        Writer out = new OutputStreamWriter(socket.getOutputStream(), HTTP.DEF_CONTENT_CHARSET);
        out.write("GET / HTTP/1.1\r\n");
        out.write("Host: " + target.toHostString() + "\r\n");
        out.write("Agent: whatever\r\n");
        out.write("Connection: close\r\n");
        out.write("\r\n");
        out.flush();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(socket.getInputStream(), HTTP.DEF_CONTENT_CHARSET));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    } finally {
        socket.close();
    }
}

From source file:Main.java

/**
 * Performs execution of the shell command on remote host.
 * @param host ip address or name of the host.
 * @param port telnet port/*from   w  w  w  .  j  a  v a 2s . com*/
 * @param command shell command to be executed.
 * @return true if success, false on error.
 */
public static final boolean executeRemotely(String host, int port, String command) {
    Socket socket = null;
    OutputStream os = null;

    try {
        socket = new Socket(host, port);

        os = socket.getOutputStream();

        os.write(command.getBytes());
        os.flush();

        return true;
    } catch (UnknownHostException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {

        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (socket != null && !socket.isClosed()) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}