List of usage examples for java.net Socket Socket
public Socket(InetAddress address, int port) throws IOException
From source file:com.annuletconsulting.homecommand.node.AsyncSend.java
@Override public Loader<String> onCreateLoader(int id, Bundle args) { AsyncTaskLoader<String> loader = new AsyncTaskLoader<String>(activity) { @Override// ww w.j a va 2s . c o m public String loadInBackground() { StringBuffer instr = new StringBuffer(); try { Socket connection = new Socket(ipAddr, port); BufferedOutputStream bos = new BufferedOutputStream(connection.getOutputStream()); OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII"); osw.write(formatJSON(command.toUpperCase())); osw.write(13); osw.flush(); BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); InputStreamReader isr = new InputStreamReader(bis, "US-ASCII"); int c; while ((c = isr.read()) != 13) instr.append((char) c); isr.close(); bis.close(); osw.close(); bos.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } return instr.toString(); } }; return loader; }
From source file:info.varden.irclinqed.dcc.FileReceiveThread.java
@Override public void run() { try {/*from w w w. j av a 2 s . c o m*/ this.il.keyListenerQueue.add(this); this.gfp = new GuiFileProgress(this.packet.il, this); this.packet.il.guiQueue.add(this.gfp); if (!this.file.getParentFile().exists()) { this.file.getParentFile().mkdirs(); } if (!this.file.exists()) { this.file.createNewFile(); } Socket s = new Socket(this.host, this.port); InputStream i = s.getInputStream(); this.cos = new CountingOutputStream(new FileOutputStream(this.file)); byte[] buff = new byte[1024]; int k = -1; while ((k = i.read(buff)) > -1 && !this.cancel) { this.cos.write(buff, 0, k); s.getOutputStream().write(ByteBuffer.allocate(4).putInt((int) getByteCount()).array()); } s.shutdownInput(); s.shutdownOutput(); s.close(); this.cos.close(); this.gfp.unload(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Clases.cCifrado.java
public String pedirClave(String dato) { String clave = ""; Scanner sc = new Scanner(System.in); Socket socket;// w w w .ja v a2 s . c o m try { String ipServidor = receptor; int pto = puerto; System.out.println("conectando con el servidor..."); socket = new Socket(ipServidor, pto); Scanner entrada = new Scanner(socket.getInputStream()); PrintStream salidaServer = new PrintStream(socket.getOutputStream()); salidaServer.println(dato); clave = entrada.nextLine(); System.out.println("Dato recibido: " + clave); socket.close(); } catch (IOException ex) { System.err.println("Cliente> " + ex.getMessage()); } return clave; }
From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java
/** * Sets up the socket connection to a given host * //from w w w .j a v a 2 s.com * @param host * of type String * @param port * of type int * @return Socket * @throws IntegrationException * when the socket couldn't be created */ public static Socket prepareSocket(String host, int port) throws IntegrationException { try { return new Socket(host, port); } catch (IOException e) { log.error("prepare socket", e); throw new IntegrationException("Cannot connect to " + host + ":" + port + ". Cause: " + e.getMessage()); } }
From source file:org.mariotaku.twidere.util.net.HostResolvedSocketFactory.java
@Override public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException { final String resolvedHost = resolver.resolve(host); if (resolvedHost != null && !resolvedHost.equals(host)) { if (InetAddressUtils.isIPv6Address(resolvedHost)) { final byte[] resolvedAddress = Inet6Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); } else if (InetAddressUtils.isIPv4Address(resolvedHost)) { final byte[] resolvedAddress = Inet4Address.getByName(resolvedHost).getAddress(); return new Socket(InetAddress.getByAddress(host, resolvedAddress), port); }//from ww w.ja va 2 s . co m } return defaultFactory.createSocket(host, port, localHost, localPort); }
From source file:com.impetus.kundera.examples.twitter.TwissandraTest.java
/** * Check if server running./* w w w. j av a2 s . c o m*/ * * @return true, if successful */ public static boolean checkIfServerRunning() { try { Socket socket = new Socket("127.0.0.1", 9165); return socket.getInetAddress() != null; } catch (UnknownHostException e) { return false; } catch (IOException e) { return false; } }
From source file:fitnesse.http.ResponseParser.java
public static ResponseParser performHttpRequest(String hostname, int hostPort, RequestBuilder builder) throws IOException { Socket socket = new Socket(hostname, hostPort); OutputStream socketOut = socket.getOutputStream(); InputStream socketIn = socket.getInputStream(); builder.send(socketOut);/*from www . j a va 2 s. c om*/ socketOut.flush(); try { return new ResponseParser(socketIn); } finally { socketOut.close(); socket.close(); } }
From source file:com.mulesoft.agent.monitoring.publisher.ZabbixMonitorPublisher.java
@Override public boolean flush(@NotNull Collection<List<Metric>> listOfMetrics) { Socket zabbixConnection = null; OutputStream out = null;/* ww w. j a va2 s . co m*/ BufferedReader in = null; try { for (List<Metric> metrics : listOfMetrics) { for (Metric metric : metrics) { zabbixConnection = new Socket(zabbixServer, zabbixPort); StringBuilder message = new StringBuilder(); message.append(MESSAGE_START); message.append(host); message.append(MESSAGE_MIDDLE_LEFT); message.append(metric.getName().replaceAll("\\s", "").replace(":", "")); message.append(MESSAGE_MIDDLE_RIGHT); message.append(metric.getValue()); message.append(MESSAGE_END); String s = message.toString(); byte[] chars = s.getBytes(); int length = chars.length; out = zabbixConnection.getOutputStream(); out.write(new byte[] { 'Z', 'B', 'X', 'D', '\1', (byte) (length & 0xFF), (byte) ((length >> 8) & 0x00FF), (byte) ((length >> 16) & 0x0000FF), (byte) ((length >> 24) & 0x000000FF), '\0', '\0', '\0', '\0' }); out.write(chars); out.flush(); in = new BufferedReader(new InputStreamReader(zabbixConnection.getInputStream())); LOGGER.debug("Message sent to Zabbix: " + message.toString()); } } } catch (IOException e) { LOGGER.warn("Failed to establish connection to Zabbix", e); return false; } finally { try { if (in != null) { in.close(); } if (out != null) { out.close(); } if (zabbixConnection != null) { zabbixConnection.close(); } } catch (IOException e) { } } return true; }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
/** * Test that by default we have protocol version 1.4. * //from w w w . j a v a 2 s . c o m * @throws Exception * When the test failed. */ @Test public void testDefault() throws Exception { final Agent agent = new ZabbixAgent(); // give the agent some time to open the port Thread.sleep(100); final Socket socket = new Socket(InetAddress.getLocalHost(), ZabbixAgent.DEFAULT_PORT); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write("system.property[java.version]\n"); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; in.read(buffer); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); // we'll take the rest for granted... socket.close(); agent.stop(); }
From source file:cz.zeno.miner.stratum.StratumClient.java
public StratumClient(Appender appender, String address, int port, String name, String password, boolean isScrypt) throws IOException { //initial target this.target = new byte[] { (byte) 255, (byte) 255, (byte) 255, (byte) 255 }; socket = new Socket(address, port); outputStream = socket.getOutputStream(); inputStream = socket.getInputStream(); this.name = name; this.password = password; this.appender = appender; this.isScrypt = isScrypt; }