List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:fr.enseirb.odroidx.videomanager.Uploader.java
public void doUpload(Uri myFile) { createNotification();/*from w w w . j a v a 2s . c om*/ File f = new File(myFile.getPath()); SendName(f.getName().replace(' ', '-')); Log.e(getClass().getSimpleName(), "test: " + f.exists()); if (f.exists()) { Socket s; try { Log.e(getClass().getSimpleName(), "test: " + server_ip); s = new Socket(InetAddress.getByName(server_ip), 5088);// Bug // using // variable // port OutputStream fluxsortie = s.getOutputStream(); int nb_parts = (int) (f.length() / PART_SIZE); InputStream in = new BufferedInputStream(new FileInputStream(f)); ByteArrayOutputStream byte_array = new ByteArrayOutputStream(); BufferedOutputStream buffer = new BufferedOutputStream(byte_array); byte[] to_write = new byte[PART_SIZE]; for (int i = 0; i < nb_parts; i++) { in.read(to_write, 0, PART_SIZE); buffer.write(to_write); buffer.flush(); fluxsortie.write(byte_array.toByteArray()); byte_array.reset(); if ((i % 250) == 0) { mBuilder.setProgress(nb_parts, i, false); mNotifyManager.notify(NOTIFY_ID, mBuilder.getNotification()); } } int remaining = (int) (f.length() - nb_parts * PART_SIZE); in.read(to_write, 0, remaining); buffer.write(to_write); buffer.flush(); fluxsortie.write(byte_array.toByteArray()); byte_array.reset(); buffer.close(); fluxsortie.close(); in.close(); s.close(); } catch (ConnectException e) { if (STATUS != HTTP_SERVER) STATUS = CONNECTION_ERROR; e.printStackTrace(); } catch (UnknownHostException e) { if (STATUS != HTTP_SERVER) STATUS = UNKNOWN; Log.i(getClass().getSimpleName(), "Unknown host"); e.printStackTrace(); } catch (IOException e) { if (STATUS != HTTP_SERVER) STATUS = CONNECTION_ERROR; e.printStackTrace(); } } }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
/** * Test the we can ping the agent.//from ww w. j av a 2 s . com * * @throws Exception * When the test failed. */ @Test public void testPing() throws Exception { final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent(); // give the agent some time to open the port Thread.sleep(100); final Socket socket = new Socket(InetAddress.getLocalHost(), org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write("agent.ping\n"); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; final int read = in.read(buffer); assertEquals(14, read); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); assertEquals('1', buffer[13]); // we'll take the rest for granted... socket.close(); agent.stop(); }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
/** * Test robustness.// w ww . j av a2 s . co m * * @throws Exception * When the test failed. */ @Test public void testMissingArgument() throws Exception { final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent(); // give the agent some time to open the port Thread.sleep(100); final Socket socket = new Socket(InetAddress.getLocalHost(), org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write("jmx\n"); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; final int read = in.read(buffer); assertEquals(29, read); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); assertEquals('N', buffer[17]); assertEquals('O', buffer[18]); assertEquals('T', buffer[19]); // we'll take the rest for granted... socket.close(); agent.stop(); }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
/** * Test robustness.//from w ww . j av a2 s. c o m * * @throws Exception * When the test failed. */ @Test public void testMissingOpen() throws Exception { final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent(); // give the agent some time to open the port Thread.sleep(100); final Socket socket = new Socket(InetAddress.getLocalHost(), org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write("jmx(foo]\n"); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; final int read = in.read(buffer); assertEquals(29, read); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); assertEquals('N', buffer[17]); assertEquals('O', buffer[18]); assertEquals('T', buffer[19]); // we'll take the rest for granted... socket.close(); agent.stop(); }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
/** * Test robustness./* w w w . j a va2 s .c o m*/ * * @throws Exception * When the test failed. */ @Test public void testMissingClose() throws Exception { final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent(); // give the agent some time to open the port Thread.sleep(100); final Socket socket = new Socket(InetAddress.getLocalHost(), org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write("jmx[foo\n"); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; final int read = in.read(buffer); assertEquals(29, read); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); assertEquals('N', buffer[17]); assertEquals('O', buffer[18]); assertEquals('T', buffer[19]); // we'll take the rest for granted... socket.close(); agent.stop(); }
From source file:com.yahoo.pulsar.testclient.LoadSimulationClient.java
private void handle(final Socket socket) throws Exception { final DataInputStream inputStream = new DataInputStream(socket.getInputStream()); int command;/*from w w w . j a v a2 s . c o m*/ while ((command = inputStream.read()) != -1) { handle((byte) command, inputStream, new DataOutputStream(socket.getOutputStream())); } }
From source file:com.quigley.zabbixj.agent.active.ActiveThread.java
private void sendMetrics(int delay, List<String> keyList) throws Exception { if (log.isDebugEnabled()) { String message = "Sending metrics for delay '" + delay + "' with keys: "; for (int i = 0; i < keyList.size(); i++) { if (i > 0) { message += ", "; }//from w w w . j a v a2 s .co m message += keyList.get(i); } log.debug(message); } long clock = System.currentTimeMillis() / 1000; JSONObject metrics = new JSONObject(); metrics.put("request", "agent data"); JSONArray data = new JSONArray(); for (String keyName : keyList) { JSONObject key = new JSONObject(); key.put("host", hostName); key.put("key", keyName); try { Object value = metricsContainer.getMetric(keyName); key.put("value", value.toString()); } catch (Exception e) { key.put("value", "ZBX_NOTSUPPORTED"); } key.put("clock", "" + clock); data.put(key); } metrics.put("data", data); metrics.put("clock", "" + clock); Socket socket = new Socket(serverAddress, serverPort); InputStream input = socket.getInputStream(); OutputStream output = socket.getOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); output.write(getRequest(metrics)); output.flush(); byte[] buffer = new byte[10240]; int read = 0; while ((read = input.read(buffer, 0, 10240)) != -1) { baos.write(buffer, 0, read); } socket.close(); JSONObject response = getResponse(baos.toByteArray()); if (response.getString("response").equals("success")) { if (log.isDebugEnabled()) { log.debug("The server reported success '" + response.getString("info") + "'."); } } else { log.error("Failure!"); } lastChecked.put(delay, clock); }
From source file:br.gov.frameworkdemoiselle.monitoring.internal.implementation.zabbix.ZabbixSender.java
/** * @param key/* w w w .ja va 2 s . com*/ * @param value * @throws IOException */ private void send(final String key, final String value) throws IOException { final long start = System.currentTimeMillis(); final StringBuilder message = new StringBuilder(head); message.append(encodeBase64(key)); message.append(middle); message.append(encodeBase64(value == null ? "" : value)); message.append(tail); logger.debug(bundle.getString("zabbix-sender-sending-message", this.zabbixHost, key, value)); logger.trace(bundle.getString("zabbix-sender-detailed-message", message)); Socket socket = null; OutputStreamWriter out = null; InputStream in = null; try { socket = new Socket(zabbixServer, zabbixPort); socket.setSoTimeout(TIMEOUT); out = new OutputStreamWriter(socket.getOutputStream()); out.write(message.toString()); out.flush(); in = socket.getInputStream(); final int read = in.read(response); final String resp = new String(response); logger.debug(bundle.getString("zabbix-sender-received-response", resp)); if (read != 2 || response[0] != 'O' || response[1] != 'K') { logger.warn(bundle.getString("zabbix-sender-unexpected-response", key, resp)); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } if (socket != null) { socket.close(); } } final long elapsed = System.currentTimeMillis() - start; logger.trace(bundle.getString("zabbix-sender-message-sent", elapsed)); }
From source file:net.arccotangent.pacchat.net.KeyUpdateClient.java
public void run() { Socket socket; BufferedReader input;//from ww w . ja v a2s. c o m BufferedWriter output; kuc_log.i("Connecting to server at " + server_ip); try { socket = new Socket(); socket.connect(new InetSocketAddress(InetAddress.getByName(server_ip), Server.PORT), 1000); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); } catch (SocketTimeoutException e) { kuc_log.e("Connection to server timed out!"); e.printStackTrace(); return; } catch (ConnectException e) { kuc_log.e("Connection to server was refused!"); e.printStackTrace(); return; } catch (UnknownHostException e) { kuc_log.e("You entered an invalid IP address!"); e.printStackTrace(); return; } catch (IOException e) { kuc_log.e("Error connecting to server!"); e.printStackTrace(); return; } try { kuc_log.i("Requesting a key update."); output.write("302 request key update"); output.newLine(); output.flush(); kuc_log.i("Awaiting response from server."); String update = input.readLine(); switch (update) { case "303 update": kuc_log.i("Server accepted update request, sending public key."); String pubkeyB64 = Base64.encodeBase64String(Main.getKeypair().getPublic().getEncoded()); output.write(pubkeyB64); output.newLine(); output.flush(); output.close(); break; case "304 no update": kuc_log.i("Server rejected update request, closing connection."); input.close(); output.close(); break; case "305 update unavailable": kuc_log.i("Server cannot update at this time, try again later."); input.close(); output.close(); break; default: kuc_log.i("Server sent back invalid response"); input.close(); output.close(); break; } } catch (IOException e) { kuc_log.e("Error in key update request!"); e.printStackTrace(); } }
From source file:de.ailis.oneinstance.OneInstance.java
/** * Runs the client./* www.j a v a 2 s . c o m*/ * * @param socket * The client socket. * @param args * The command-line arguments. * @return True if server accepted the new instance, false if not. * @throws IOException * When communication with the server fails. */ private boolean runClient(Socket socket, String[] args) throws IOException { // Send serialized command-line argument list to the server. ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); out.writeObject(new File(".").getCanonicalFile()); out.writeObject(args); out.flush(); // Read response from server BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8")); String response = in.readLine(); // If response is "exit" then don't start new instance. Any other // reply will allow the new instance. return response == null || !response.equals("exit"); }