@Override public void run() { try (Socket socket = new Socket(InetAddress.getLocalHost(), serverPort)) { OutputStream outputStream = socket.getOutputStream(); PrintWriter writer = new PrintWriter(outputStream); sendLogRequests(writer, socket.getInputStream()); } catch (IOException e) { LOGGER.error("error sending requests", e); throw new RuntimeException(e); } }
public static boolean pingHost(String host, int port, int timeout) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(host, port), timeout); return true; } catch (IOException e) { return false; // Either timeout or unreachable or failed DNS lookup. } }
/** * 激活网络 */ private Socket connect(InetSocketAddress address) throws IOException { final Socket socket = new Socket(); socket.setSoTimeout(0); socket.connect(address, _1MIN); socket.setKeepAlive(true); socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); return socket; }
public static void kill(String host, int port) { Socket s = null; try { byte[] reqBytes = new byte[4]; ByteBuffer req = ByteBuffer.wrap(reqBytes); req.putInt(ByteBuffer.wrap("kill".getBytes()).getInt()); s = new Socket(); s.setSoLinger(false, 10); s.setSoTimeout(20000); s.connect(new InetSocketAddress(host, port)); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); os.write(reqBytes); byte[] resBytes = new byte[4]; int rc = is.read(resBytes); String retv = new String(resBytes); System.out.println("rc=" + rc + " retv=" + retv); } catch (IOException e) { LOG.warn("Unexpected exception", e); } finally { if (s != null) { try { s.close(); } catch (IOException e) { LOG.warn("Unexpected exception", e); } } } }
public static boolean checkHealth(String ip, int port) { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress(ip, port), 1000); socket.close(); return true; } catch (IOException e) { return false; } }
public static void main(String args[]) { if (2 != args.length) { System.err.println(MSG_USAGE); System.exit(E_USAGE); } try { Socket s = new Socket( InetAddress.getByName(args[0]), Integer.valueOf(args[1]).intValue()); s.close(); System.exit(0); } catch (Throwable t) { System.err.println(t.toString()); System.exit(E_FAILED); } } }
try { Socket x = new Socket("1.1.1.1", 6789); x.getInputStream().read() } catch (IOException e) { System.err.println("Connection could not be established, please try again later!") }
/** * Tell the server to exit */ public void sendQuit() throws IOException { Socket socket = new Socket(host, port); Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8"); out.write("quit\n"); out.flush(); socket.close(); }
private void createConnection() throws IOException { client = new Socket(hostName, port); client.setKeepAlive(true); client.setTcpNoDelay(true); outputStream = client.getOutputStream(); }
/** * Returs a Tree from the server connected to at host:port. */ public Tree getTree(String query) throws IOException { Socket socket = new Socket(host, port); Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8"); out.write("tree " + query + "\n"); out.flush(); ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); Object o; try { o = ois.readObject(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } if (!(o instanceof Tree)) { throw new IllegalArgumentException("Expected a tree"); } Tree tree = (Tree) o; socket.close(); return tree; }
public static void dump(String host, int port) { Socket s = null; try { byte[] reqBytes = new byte[4]; ByteBuffer req = ByteBuffer.wrap(reqBytes); req.putInt(ByteBuffer.wrap("dump".getBytes()).getInt()); s = new Socket(); s.setSoLinger(false, 10); s.setSoTimeout(20000); s.connect(new InetSocketAddress(host, port)); InputStream is = s.getInputStream(); OutputStream os = s.getOutputStream(); os.write(reqBytes); byte[] resBytes = new byte[1024]; int rc = is.read(resBytes); String retv = new String(resBytes); System.out.println("rc=" + rc + " retv=" + retv); } catch (IOException e) { LOG.warn("Unexpected exception", e); } finally { if (s != null) { try { s.close(); } catch (IOException e) { LOG.warn("Unexpected exception", e); } } } }
public static Future<Boolean> portIsOpen(final ExecutorService es, final String ip, final int port, final int timeout) { return es.submit(new Callable<Boolean>() { @Override public Boolean call() { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress(ip, port), timeout); socket.close(); return true; } catch (Exception ex) { return false; } } }); }
private static boolean isPortOpen(String host, Integer port) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), 1000); return true; } catch (ConnectException | SocketTimeoutException e) { return false; } catch (IOException e) { throw new UncheckedIOException(e); } } }
/** * Tokenize the text according to the parser's tokenizer, * return it as whitespace tokenized text. */ public String getTokenizedText(String query) throws IOException { Socket socket = new Socket(host, port); Writer out = new OutputStreamWriter(socket.getOutputStream(), "utf-8"); out.write("tokenize " + query + "\n"); out.flush(); String result = readResult(socket); socket.close(); return result; }
public SocketConnector( InetAddress address, int port ) throws IOException { this.sock = new Socket(address, port); remoteAddress = sock.getRemoteSocketAddress(); // for info purposes // Disable Nagle's buffering so data goes out when we // put it there. sock.setTcpNoDelay(true); in = sock.getInputStream(); out = sock.getOutputStream(); connected.set(true); }
@Override public void prepare(Map<String, Object> topoConf, Object registrationArgument, TopologyContext context, IErrorReporter errorReporter) { String[] parts = ((String) registrationArgument).split(":", 2); host = parts[0]; port = Integer.valueOf(parts[1]); try { socket = new Socket(host, port); out = socket.getOutputStream(); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void open(Map<String, Object> conf, TopologyContext context, SpoutOutputCollector collector) { this.collector = collector; this.queue = new LinkedBlockingDeque<>(); this.emitted = new HashMap<>(); this.objectMapper = new ObjectMapper(); try { socket = new Socket(host, port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); } catch (IOException e) { throw new RuntimeException("Error opening socket: host " + host + " port " + port); } readerThread = new Thread(new SocketSpout.SocketReaderRunnable()); readerThread.start(); }