List of usage examples for java.net Socket isConnected
public boolean isConnected()
From source file:org.wso2.carbon.business.messaging.core.internal.BrokerServiceComponent.java
/** * <p>//from www .java 2 s.c o m * check whether the tcp port has started. some times the server started thread may return * before Qpid server actually bind to the tcp port. in that case there are some connection * time out issues. * </p> * * @throws ConfigurationException */ private void startAMQPServer() throws ConfigurationException { boolean isServerStarted = false; int port; if (brokerService.getIfSSLOnly()) { port = brokerService.getAMQPSSLPort(); } else { port = brokerService.getAMQPPort(); } if (AndesConfigurationManager.<Boolean>readValue(AndesConfiguration.TRANSPORTS_AMQP_ENABLED)) { while (!isServerStarted) { Socket socket = null; try { InetAddress address = InetAddress.getByName(getAMQPTransportBindAddress()); socket = new Socket(address, port); log.info("AMQP Host Address : " + address.getHostAddress() + " Port : " + port); isServerStarted = socket.isConnected(); if (isServerStarted) { log.info("Successfully connected to AMQP server " + "on port " + port); } } catch (IOException e) { //There will be a continuous retry effort to start the qpid server, if an error occurs the // exception will not be propagated further log.error("Wait until Qpid server starts on port " + port, e); try { Thread.sleep(500); } catch (InterruptedException ignore) { // Ignore } } finally { try { if ((socket != null) && (socket.isConnected())) { socket.close(); } } catch (IOException e) { //There will be a continuous retry effort to close the socket, if an error occurs the // exception will not be propagated further log.error("Can not close the socket which is used to check the server " + "status ", e); } } } } else { log.warn("AMQP Transport is disabled as per configuration."); } }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testDataRegistration() throws Exception { Server server = new Server(); assertTrue(server != null);/*from w w w . j a v a 2 s . co m*/ server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); JSONObject json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); server.stopServer(); String shutdownAction = in.readLine(); jsonObject = new JSONObject(shutdownAction); assertEquals("shutdown", jsonObject.get("action")); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testGetData() throws Exception { Server server = new Server(); assertTrue(server != null);//from w w w . j a v a 2 s .com server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); //reg few test data JSONObject json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); in.readLine(); json = new JSONObject(); json.put("action", "get_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); server.stopServer(); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testGetDataList() throws Exception { Server server = new Server(); assertTrue(server != null);/*from www. j av a 2 s. c om*/ server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); PrintWriter out2 = new PrintWriter(sock.getOutputStream(), true); //reg few test data JSONObject json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out2.println(json.toString()); out2.flush(); in.readLine(); json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name2"); out2.println(json.toString()); out2.flush(); in.readLine(); json = new JSONObject(); json.put("action", "get_data_list"); out.println(json.toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); server.stopServer(); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testSubscribe() throws Exception { Server server = new Server(); assertTrue(server != null);// w w w. j ava2s.c o m server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); JSONObject json = new JSONObject(); json.put("action", "subscribe"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("error", jsonObject.get("status")); //reg few test data json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); in.readLine(); json = new JSONObject(); json.put("action", "subscribe"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); data = in.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); server.stopServer(); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testClientAccept() throws Exception { Server server = new Server(); assertTrue(server != null);// w ww . j a v a 2 s.c om server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); out.println(new JSONObject().put("action", "test").toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("test", jsonObject.get("msg")); Socket sock2 = new Socket("localhost", server.getPort()); assertTrue(sock2 != null); BufferedReader in2 = new BufferedReader(new InputStreamReader(sock2.getInputStream())); assertTrue(in2 != null); PrintWriter out2 = new PrintWriter(sock2.getOutputStream(), true); assertTrue(out2 != null); assertTrue(sock2.isConnected()); out2.println(new JSONObject().put("action", "test").toString()); out2.flush(); String data2 = in2.readLine(); JSONObject jsonObject2 = new JSONObject(data2); assertEquals("test", jsonObject2.get("msg")); server.stopServer(); String shutdownAction = in.readLine(); String shutdownAction2 = in2.readLine(); jsonObject = new JSONObject(shutdownAction); assertEquals("shutdown", jsonObject.get("action")); jsonObject = new JSONObject(shutdownAction2); assertEquals("shutdown", jsonObject.get("action")); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testSendData() throws Exception { Server server = new Server(); assertTrue(server != null);/* w ww.j ava 2s .c o m*/ server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); Socket sock2 = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in2 = new BufferedReader(new InputStreamReader(sock2.getInputStream())); assertTrue(in != null); PrintWriter out2 = new PrintWriter(sock2.getOutputStream(), true); assertTrue(out != null); assertTrue(sock2.isConnected()); //reg few test data JSONObject json = new JSONObject(); json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); in.readLine(); json = new JSONObject(); json.put("action", "subscribe"); json.put("name", "test_name"); out2.println(json.toString()); out2.flush(); String data = in2.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); json = new JSONObject(); json.put("action", "send_data"); json.put("name", "test_name"); json.put("value", "test_value"); out.println(json.toString()); out.flush(); in.readLine(); data = in2.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); json = new JSONObject(); json.put("action", "send_data"); json.put("name", "test_name"); json.put("value", "test_value2"); out.println(json.toString()); out.flush(); in.readLine(); data = in2.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); assertEquals("test_value2", jsonObject.get("value")); }
From source file:org.socsimnet.server.test.ServerTest.java
@Test public void testUnsubscribe() throws Exception { Server server = new Server(); assertTrue(server != null);/*from w ww.ja v a 2 s.c o m*/ server.start(); Socket sock = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream())); assertTrue(in != null); PrintWriter out = new PrintWriter(sock.getOutputStream(), true); assertTrue(out != null); assertTrue(sock.isConnected()); Socket sock2 = new Socket("localhost", server.getPort()); assertTrue(sock != null); BufferedReader in2 = new BufferedReader(new InputStreamReader(sock2.getInputStream())); assertTrue(in != null); PrintWriter out2 = new PrintWriter(sock2.getOutputStream(), true); assertTrue(out != null); assertTrue(sock2.isConnected()); JSONObject json = new JSONObject(); json.put("action", "unsubscribe"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); String data = in.readLine(); JSONObject jsonObject = new JSONObject(data); assertEquals("error", jsonObject.get("status")); //reg few test data json = new JSONObject(); json.put("action", "register_data"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); in.readLine(); json = new JSONObject(); json.put("action", "subscribe"); json.put("name", "test_name"); out.println(json.toString()); out.flush(); data = in.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); json = new JSONObject(); json.put("action", "subscribe"); json.put("name", "test_name"); out2.println(json.toString()); out2.flush(); data = in2.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); json = new JSONObject(); json.put("action", "unsubscribe"); json.put("name", "test_name"); out2.println(json.toString()); out2.flush(); data = in2.readLine(); jsonObject = new JSONObject(data); assertEquals("ok", jsonObject.get("status")); server.stopServer(); }
From source file:com.cws.esolutions.core.utils.NetworkUtils.java
/** * Creates an telnet connection to a target host and port number. Silently * succeeds if no issues are encountered, if so, exceptions are logged and * re-thrown back to the requestor./*from w w w . j a va 2 s . c o m*/ * * If an exception is thrown during the <code>socket.close()</code> operation, * it is logged but NOT re-thrown. It's not re-thrown because it does not indicate * a connection failure (indeed, it means the connection succeeded) but it is * logged because continued failures to close the socket could result in target * system instability. * * @param hostName - The target host to make the connection to * @param portNumber - The port number to attempt the connection on * @param timeout - The timeout for the connection * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing */ public static final synchronized void executeTelnetRequest(final String hostName, final int portNumber, final int timeout) throws UtilityException { final String methodName = NetworkUtils.CNAME + "#executeTelnetRequest(final String hostName, final int portNumber, final int timeout) throws UtilityException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug(hostName); DEBUGGER.debug("portNumber: {}", portNumber); DEBUGGER.debug("timeout: {}", timeout); } Socket socket = null; try { synchronized (new Object()) { if (InetAddress.getByName(hostName) == null) { throw new UnknownHostException("No host was found in DNS for the given name: " + hostName); } InetSocketAddress socketAddress = new InetSocketAddress(hostName, portNumber); socket = new Socket(); socket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(timeout)); socket.setSoLinger(false, 0); socket.setKeepAlive(false); socket.connect(socketAddress, (int) TimeUnit.SECONDS.toMillis(timeout)); if (!(socket.isConnected())) { throw new ConnectException("Failed to connect to host " + hostName + " on port " + portNumber); } PrintWriter pWriter = new PrintWriter(socket.getOutputStream(), true); pWriter.println(NetworkUtils.TERMINATE_TELNET + NetworkUtils.CRLF); pWriter.flush(); pWriter.close(); } } catch (ConnectException cx) { throw new UtilityException(cx.getMessage(), cx); } catch (UnknownHostException ux) { throw new UtilityException(ux.getMessage(), ux); } catch (SocketException sx) { throw new UtilityException(sx.getMessage(), sx); } catch (IOException iox) { throw new UtilityException(iox.getMessage(), iox); } finally { try { if ((socket != null) && (!(socket.isClosed()))) { socket.close(); } } catch (IOException iox) { // log it - this could cause problems later on ERROR_RECORDER.error(iox.getMessage(), iox); } } }
From source file:org.wso2.carbon.andes.internal.QpidServiceComponent.java
/** * check whether the tcp port has started. some times the server started thread may return * before Qpid server actually bind to the tcp port. in that case there are some connection * time out issues./*from ww w. j a v a 2 s. c om*/ * * @throws ConfigurationException */ private void startAMQPServer() throws ConfigurationException { boolean isServerStarted = false; int port; if (qpidServiceImpl.getIfSSLOnly()) { port = qpidServiceImpl.getAMQPSSLPort(); } else { port = qpidServiceImpl.getAMQPPort(); } if (AndesConfigurationManager.<Boolean>readValue(AndesConfiguration.TRANSPORTS_AMQP_ENABLED)) { while (!isServerStarted) { Socket socket = null; try { InetAddress address = InetAddress.getByName(getAMQPTransportBindAddress()); socket = new Socket(address, port); log.info("AMQP Host Address : " + address.getHostAddress() + " Port : " + port); isServerStarted = socket.isConnected(); if (isServerStarted) { log.info("Successfully connected to AMQP server " + "on port " + port); } } catch (IOException e) { log.error("Wait until Qpid server starts on port " + port, e); try { Thread.sleep(500); } catch (InterruptedException ignore) { // Ignore } } finally { try { if ((socket != null) && (socket.isConnected())) { socket.close(); } } catch (IOException e) { log.error("Can not close the socket which is used to check the server " + "status ", e); } } } } else { log.warn("AMQP Transport is disabled as per configuration."); } }