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:totalcross.android.ConnectionManager4A.java

public static boolean isInternetAccessible() {
    try {//from w w  w .  j av a 2  s.c  o  m
        InetSocketAddress isa = new InetSocketAddress(InetAddress.getByName("www.google.com"), 80);
        Socket s = new Socket();
        s.connect(isa, 30 * 1000);
        s.close();
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

public static void sendStopSignal(int port) {
    try {/*from  w ww. j av  a 2 s.co  m*/
        Socket s = new Socket(InetAddress.getByName("127.0.0.1"), port);
        OutputStream out = s.getOutputStream();
        System.err.println("sending server stop request");
        out.write(("\r\n").getBytes());
        out.flush();
        s.close();
    } catch (Exception e) {
        // can happen when called twice by jvm shutdown hook
        System.err.println("stop monitor thread has terminated");
    }
}

From source file:com.sharneng.net.NetUtils.java

/**
 * Quietly close a {@linkplain Socket} object. It tries to shutdown the input and output of the socket before close.
 * Exceptions are ignored./*from ww  w .j  av  a2  s  .c o m*/
 * 
 * @param socket
 *            the Socket object to close
 * @see #shutdown(Socket)
 */
public static void close(Socket socket) {
    if (socket == null)
        return;

    if (!socket.isClosed()) {
        shutdown(socket);
        try {
            socket.close();
        } catch (Throwable e) {
            log.debug(e.getMessage(), e);
        }
    }
}

From source file:org.openbaton.autoscaling.utils.Utils.java

public static boolean available(String ip, String port) {
    try {/*from w  w w  . j  a v  a2s.co m*/
        Socket s = new Socket(ip, Integer.parseInt(port));
        log.info("NFVO is listening on port " + port + " at " + ip);
        s.close();
        return true;
    } catch (IOException ex) {
        // The remote host is not listening on this port
        log.warn("NFVO is not reachable on port " + port + " at " + ip);
        return false;
    }
}

From source file:org.openbaton.vnfm.utils.Utils.java

public static boolean available(String ip, String port) {
    try {// www.j  a v a  2  s  . co m
        Socket s = new Socket(ip, Integer.parseInt(port));
        s.close();
        return true;
    } catch (IOException ex) {
        // The remote host is not listening on this port
        return false;
    }
}

From source file:Main.java

/**
 *
 * Get a tcp socket connection to specified IP address and port proxied by adb
 *
 * The proxying is transparent, e.g. if a socket is returned, then it can be written to and
 * read from as if it is directly connected to the target
 *
 * @param remoteAddress IP address of the host to connect to
 * @param remotePort port of the host to connect to
 * @return a valid Socket instance if successful, null otherwise
 *///from   www  . jav  a 2s  . co m
public static Socket getForwardedSocket(int remoteAddress, int remotePort) {
    try {
        Socket socket = new Socket(ADB_HOST, ADB_PORT);
        String cmd = "tcp:" + remotePort + ":" + convert(remoteAddress);
        if (!sendAdbCmd(socket.getInputStream(), socket.getOutputStream(), cmd)) {
            socket.close();
            return null;
        }
        return socket;
    } catch (IOException ioe) {
        Log.w(LOGTAG, "error creating adb socket", ioe);
        return null;
    }
}

From source file:eu.stratosphere.nephele.util.IOUtils.java

/**
 * Closes the socket ignoring {@link IOException}.
 * /*from   ww  w .j av a 2  s . c om*/
 * @param sock
 *        the socket to close
 */
public static void closeSocket(final Socket sock) {
    // avoids try { close() } dance
    if (sock != null) {
        try {
            sock.close();
        } catch (IOException ignored) {
        }
    }
}

From source file:PoolServerBase.java

static public void startServer(int port) {
    ServerSocket ssock;/*from  ww w  .  ja  va 2 s .c  o  m*/
    Socket sock;
    try {
        ssock = new ServerSocket(port);
        while (true) {
            Socket esock = null;
            try {
                esock = ssock.accept();
                while (listEmpty())
                    yield();
                assignThread(esock);
            } catch (Exception e) {
                try {
                    esock.close();
                } catch (Exception ec) {
                }
            }
        }
    } catch (IOException e) {
    }
}

From source file:com.codefollower.lealone.omid.TestUtils.java

public static void waitForSocketNotListening(String host, int port, int sleepTimeMillis)
        throws UnknownHostException, IOException, InterruptedException {
    while (true) {
        Socket sock = null;
        try {/*w  w w.j  av  a  2 s.  c o m*/
            sock = new Socket(host, port);
        } catch (IOException e) {
            // ignore as this is expected
            break;
        } finally {
            if (sock != null) {
                sock.close();
            }
        }
        Thread.sleep(sleepTimeMillis);
        LOG.info("Host " + host + ":" + port + " is up");
    }
}

From source file:Main.java

/**
 * Unconditionally close a <code>Socket</code>.
 * <p/>//from   w  ww  .ja va  2 s . c  om
 * Equivalent to {@link Socket#close()}, except any exceptions will be ignored.
 * This is typically used in finally blocks.
 * <p/>
 * Example code:
 * <pre>
 *   Socket socket = null;
 *   try {
 *       socket = new Socket("http://www.foo.com/", 80);
 *       // process socket
 *       socket.close();
 *   } catch (Exception e) {
 *       // error handling
 *   } finally {
 *       IOUtils.closeQuietly(socket);
 *   }
 * </pre>
 *
 * @param sock the Socket to close, may be null or already closed
 * @since 2.0
 */
public static void closeQuietly(Socket sock) {
    if (sock != null) {
        try {
            sock.close();
        } catch (IOException ioe) {
            // ignored
        }
    }
}