List of usage examples for java.net Socket isConnected
public boolean isConnected()
From source file:Main.java
public static void main(String[] args) throws Exception { Socket client = new Socket("google.com", 80); System.out.println(client.isConnected()); client.close();/*from w ww .java2s .co m*/ }
From source file:flink.iso8583.example.Client.java
public static void main(String[] args) throws Exception { Random rng = new Random(System.currentTimeMillis()); log.debug("Reading config"); mfact = ConfigParser.createFromClasspathConfig("flink/iso8583/example/config.xml"); mfact.setAssignDate(true);//from ww w.ja v a2 s . co m mfact.setTraceNumberGenerator(new SimpleTraceGenerator((int) (System.currentTimeMillis() % 10000))); log.debug("Connecting to server"); Socket sock = new Socket("localhost", 9999); //Send 10 messages, then wait for the responses Client reader = new Client(sock); reader.start(); for (int i = 0; i < 10; i++) { IsoMessage req = mfact.newMessage(0x200); req.setValue(4, amounts[rng.nextInt(amounts.length)], IsoType.AMOUNT, 0); req.setValue(12, req.getObjectValue(7), IsoType.TIME, 0); req.setValue(13, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(15, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(17, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(37, new Long(System.currentTimeMillis() % 1000000), IsoType.NUMERIC, 12); req.setValue(41, data[rng.nextInt(data.length)], IsoType.ALPHA, 16); req.setValue(48, data[rng.nextInt(data.length)], IsoType.LLLVAR, 0); pending.put(req.getField(11).toString(), req); log.debug("Sending request " + req.getField(11)); req.write(sock.getOutputStream(), 2); } log.debug("Waiting for responses"); while (pending.size() > 0 && sock.isConnected()) { sleep(500); } reader.interrupt(); sock.close(); log.debug("DONE."); }
From source file:j8583.example.Client.java
public static void main(String[] args) throws Exception { Random rng = new Random(System.currentTimeMillis()); log.debug("Reading config"); mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml"); mfact.setAssignDate(true);/* w w w .j a v a 2s. com*/ mfact.setTraceNumberGenerator(new SimpleTraceGenerator((int) (System.currentTimeMillis() % 10000))); System.err.println("Connecting to server"); Socket sock = new Socket("localhost", 9999); // Send 10 messages, then wait for the responses Client client = new Client(sock); Thread reader = new Thread(client, "j8583-client"); reader.start(); for (int i = 0; i < 10; i++) { IsoMessage req = mfact.newMessage(0x200); req.setValue(4, amounts[rng.nextInt(amounts.length)], IsoType.AMOUNT, 0); req.setValue(12, req.getObjectValue(7), IsoType.TIME, 0); req.setValue(13, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(15, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(17, req.getObjectValue(7), IsoType.DATE4, 0); req.setValue(37, System.currentTimeMillis() % 1000000, IsoType.NUMERIC, 12); req.setValue(41, data[rng.nextInt(data.length)], IsoType.ALPHA, 16); req.setValue(48, data[rng.nextInt(data.length)], IsoType.LLLVAR, 0); pending.put(req.getField(11).toString(), req); System.err.println(String.format("Sending request %s", req.getField(11))); req.write(sock.getOutputStream(), 2); } log.debug("Waiting for responses"); while (pending.size() > 0 && sock.isConnected()) { Thread.sleep(500); } client.stop(); reader.interrupt(); log.debug("DONE."); }
From source file:mc.lib.network.NetworkHelper.java
private static boolean testConnection(String url) { Socket s = null; boolean res = false; try {//from w ww . j av a 2 s. c o m s = new Socket(url, 80); if (s.isBound() && s.isConnected()) { res = true; } } catch (IOException e) { Log.e(LOGTAG, "Error on testConnection to " + url, e); } finally { if (s != null) StreamHelper.close(s); } return res; }
From source file:org.wso2.carbon.automation.engine.frameworkutils.ClientConnectionUtil.java
/** * @param port The port that needs to be checked * @param timeout The timeout waiting for the port to open * @param verbose if verbose is set to true, * @throws RuntimeException if the port is not opened within the {@link #TIMEOUT} *//*from w w w . j a v a 2 s . c om*/ public static void waitForPort(int port, long timeout, boolean verbose, String hostName) throws RuntimeException { long startTime = System.currentTimeMillis(); boolean isPortOpen = false; while (!isPortOpen && (System.currentTimeMillis() - startTime) < timeout) { Socket socket = null; try { InetAddress address = InetAddress.getByName(hostName); socket = new Socket(address, port); isPortOpen = socket.isConnected(); if (isPortOpen) { if (verbose) { log.info("Successfully connected to the server on port " + port); } return; } } catch (IOException e) { if (verbose) { log.info("Waiting until server starts on port " + port); } try { Thread.sleep(1000); } catch (InterruptedException ignored) { } } 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); } } } throw new RuntimeException("Port " + port + " is not open"); }
From source file:org.wso2.carbon.automation.engine.frameworkutils.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 . j av a 2 s . c om public static boolean isPortOpen(int port, String hostName) { 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:com.googlecode.icegem.cacheutils.common.Utils.java
/** * Checks if the socket of specified host and port is alive. * //from w w w. j a v a2 s. c o m * @param host * - the host. * @param port * - the port. * @return - true if alive, false otherwise. */ public static boolean isSocketAlive(String host, int port) { boolean socketAlive = false; Socket socket = null; try { socket = new Socket(host, port); socketAlive = socket.isConnected(); } catch (Throwable t) { // do nothing } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { // do nothing } } } return socketAlive; }
From source file:org.wso2.carbon.integration.framework.ClientConnectionUtil.java
/** * @param port The port that needs to be checked * @param timeout The timeout waiting for the port to open * @param verbose if verbose is set to true, * @throws RuntimeException if the port is not opened within the {@link #TIMEOUT} *///from w ww. j av a2 s . co m public static void waitForPort(int port, long timeout, boolean verbose) { long startTime = System.currentTimeMillis(); boolean isPortOpen = false; while (!isPortOpen && (System.currentTimeMillis() - startTime) < timeout) { Socket socket = null; try { InetAddress address = InetAddress.getByName("localhost"); socket = new Socket(address, port); isPortOpen = socket.isConnected(); if (isPortOpen) { if (verbose) { log.info("Successfully connected to the server on port " + port); } return; } } catch (IOException e) { if (verbose) { log.info("Waiting until server starts on port " + port); } try { Thread.sleep(1000); } catch (InterruptedException ignored) { } } 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); } } } throw new RuntimeException("Port " + port + " is not open"); }
From source file:org.wso2.carbon.integration.framework.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 *///from w ww . ja v a 2 s . com public static boolean isPortOpen(int port) { Socket socket = null; boolean isPortOpen = false; try { InetAddress address = InetAddress.getByName("localhost"); 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("Waiting until server starts on port " + port); 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.providers.ldap.util.DSManager.java
public static boolean checkSocketNotConnected() { try {//from w w w . j a va2 s. co m Socket s = new Socket("localhost", 10389); if (s.isConnected()) { logger.debug("client socket is connected (server socket bound)"); } s.close(); return false; } catch (Exception e) { logger.debug("client Socket not connected " + e.toString()); } try { ServerSocket s = new ServerSocket(10389); if (s.isBound()) { logger.debug("server socket is bound (=was therefore free)"); } s.close(); return true; } catch (Exception e) { logger.debug("Server socket already bound " + e.toString()); // e.printStackTrace(); return false; } }