List of usage examples for java.net Socket close
public synchronized void close() throws IOException
From source file:Main.java
/** * * Resolve DNS name into IP address/*from w w w .ja va 2s.c o m*/ * * @param host DNS name * @return IP address in integer format * @throws IOException */ public static int resolve(String host) throws IOException { Socket localSocket = new Socket(ADB_HOST, ADB_PORT); DataInputStream dis = new DataInputStream(localSocket.getInputStream()); OutputStream os = localSocket.getOutputStream(); int count_read = 0; if (localSocket == null || dis == null || os == null) return -1; String cmd = "dns:" + host; if (!sendAdbCmd(dis, os, cmd)) return -1; count_read = dis.readInt(); localSocket.close(); return count_read; }
From source file:org.apache.htrace.impl.HTracedProcess.java
private static StartupNotificationData readStartupNotification(ServerSocket listener) throws IOException { Socket socket = listener.accept(); try {/* w w w.j a va2 s . co m*/ InputStream in = socket.getInputStream(); ObjectMapper objectMapper = new ObjectMapper(); StartupNotificationData data = objectMapper.readValue(in, StartupNotificationData.class); return data; } finally { socket.close(); } }
From source file:com.baomidou.mybatisplus.toolkit.IOUtils.java
/** * Closes a <code>Socket</code> unconditionally. * <p>/*from www . j a va 2 s . c o m*/ * Equivalent to {@link Socket#close()}, except any exceptions will be ignored. This is typically used in finally * blocks. * <p> * Example code: * <p> * <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(final Socket sock) { if (sock != null) { try { sock.close(); } catch (final IOException ioe) { logger.error("error close io", ioe); } } }
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/*from w w w. ja v a2 s . c om*/ * @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.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++;//from w w w.ja v a 2 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:com.cyberway.issue.crawler.fetcher.FetchFTP.java
/** * Quietly closes the given socket./* ww w.j av a 2 s . c om*/ * * @param socket the socket to close */ private static void close(Socket socket) { try { socket.close(); } catch (IOException e) { logger.log(Level.WARNING, "IO error closing socket.", e); } }
From source file:Main.java
public static void handleClientRequest(Socket socket) { try {// w w w .jav a 2 s .c o m BufferedReader socketReader = null; BufferedWriter socketWriter = null; socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String inMsg = null; while ((inMsg = socketReader.readLine()) != null) { System.out.println("Received from client: " + inMsg); String outMsg = inMsg; socketWriter.write(outMsg); socketWriter.write("\n"); socketWriter.flush(); } socket.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.splout.db.benchmark.TCPTest.java
public static void tcpTest(String file, String table) throws UnknownHostException, IOException, InterruptedException, JSONSerDeException { final TCPServer server = new TCPServer(8888, file, table); Thread t = new Thread() { @Override// ww w .ja v a 2s . co m public void run() { server.serve(); } }; t.start(); while (!server.isServing()) { Thread.sleep(100); } Socket clientSocket = new Socket("localhost", 8888); DataInputStream inFromServer = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream())); try { do { // Read a record inFromServer.readUTF(); inFromServer.readInt(); inFromServer.readDouble(); inFromServer.readUTF(); } while (true); } catch (Throwable th) { th.printStackTrace(); } clientSocket.close(); server.stop(); t.interrupt(); }
From source file:com.sshtools.daemon.SshDaemon.java
/** * * * @param msg/*from w ww. j ava 2 s .c o m*/ * * @throws IOException */ public static void stop(String msg) throws IOException { try { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), ((ServerConfiguration) ConfigurationLoader.getConfiguration(ServerConfiguration.class)) .getCommandPort()); // Write the command id socket.getOutputStream().write(0x3a); // Write the length of the message (max 255) int len = (msg.length() <= 255) ? msg.length() : 255; socket.getOutputStream().write(len); // Write the message if (len > 0) { socket.getOutputStream().write(msg.substring(0, len).getBytes()); } socket.close(); } catch (IOException ioe) { ioe.printStackTrace(); } }
From source file:com.uniteddev.Unity.Login.java
public static void networkStatus(String state) { class networkService implements Runnable { public void run() { while (true) { Socket s = null; boolean connected = false; try { s = new Socket(Unity.ip, Integer.parseInt(Unity.port)); connected = true;//from w w w .jav a 2 s.c o m } catch (Exception e) { } finally { if (s != null) { try { s.close(); } catch (IOException e1) { } } } if (connected) { System.out.println("Connection to server established."); server_stat_lbl.setText("Online"); server_stat_lbl.setForeground(Color.GREEN); } else { System.out.println("Unable to establish connection."); server_stat_lbl.setText("Offline"); server_stat_lbl.setForeground(Color.RED); } try { Thread.sleep(5000); } catch (Exception e) { } ; } } } if (state == "start") { networkProbe = new Thread(new networkService()); networkProbe.start(); } else if (state == "stop") { // kill thread only if it is sleeping, otherwise // we may have open socket connections try { while (networkProbe.getState() != Thread.State.TIMED_WAITING) { networkProbe.join(1000); } networkProbe.interrupt(); } catch (InterruptedException e) { } } }