List of usage examples for java.net Socket getInputStream
public InputStream getInputStream() throws IOException
From source file:net.javacrumbs.mocksocket.SampleTest.java
@Test public void testConditionalAddress() throws Exception { byte[] mockData = new byte[] { 1, 2, 3, 4 }; expectCall().andWhenRequest(address(is("example.org:1234"))).thenReturn(data(mockData)); Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234); byte[] data = IOUtils.toByteArray(socket.getInputStream()); socket.close();//from ww w .j a va 2s . co m assertThat(data, is(mockData)); }
From source file:info.varden.irclinqed.dcc.FileReceiveThread.java
@Override public void run() { try {/* ww w .java2s .co 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:pl.edu.agh.BackgroundServiceConnection.java
/** * Compares sent hash to the one in database *///ww w . j a va 2s .com public void run() { while (true) { try { LOGGER.info("Accepting connections on port: " + serverPort); Socket client = serverSocket.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); String message = in.readLine(); DataOutputStream out = new DataOutputStream(client.getOutputStream()); LOGGER.info("Daemon message: " + message); String[] data = message.trim().split("\\|"); try { Service service = serviceDAO.getById(Integer.parseInt(data[1])); if (!data[0].equalsIgnoreCase(service.getPassword())) throw new IllegalArgumentException("Daemon password: " + data[0] + " does not match server password: " + service.getPassword()); out.writeBytes("OK\r\n"); } catch (Exception e) { LOGGER.error("Could not find server: " + message.trim(), e); out.writeBytes("ERROR\r\n"); } in.close(); out.close(); client.close(); } catch (Exception e) { LOGGER.error("Error connecting with server: " + e.getMessage()); } } }
From source file:de.root1.logiccollection.offLogicVocESP8266.java
@Override public void init() { this.tt = new TimerTask() { @Override/*from w ww. j a va 2 s . co m*/ public void run() { try { Socket s = new Socket("nodemcu1", 44444); OutputStream out = s.getOutputStream(); InputStream in = s.getInputStream(); out.write("\n".getBytes()); out.flush(); InputStreamReader isr = new InputStreamReader(in); JSONObject data = (JSONObject) JSONValue.parse(isr); isr.close(); out.close(); int voc = Integer.parseInt(data.get("voc").toString()); int tvoc = Integer.parseInt(data.get("voc").toString()); int resistance = Integer.parseInt(data.get("resistance").toString()); int status = Integer.parseInt(data.get("status").toString()); log.info("voc={}, tvoc={} resistance={} status={}", new Object[] { voc, tvoc, resistance, status }); // write(ga, String.valueOf(voc)); } catch (IOException ex) { ex.printStackTrace(); } } }; setPA("1.1.203"); t.schedule(tt, 5000, 60000); log.info("VOC ESP8266 reader is running."); }
From source file:net.javacrumbs.mocksocket.SampleTest.java
@Test public void testMultiple() throws Exception { byte[] mockData1 = new byte[] { 1, 2, 3, 4 }; byte[] mockData2 = new byte[] { 1, 2, 3, 4 }; expectCall().andReturn(data(mockData1)).andReturn(data(mockData2)); Socket socket1 = SocketFactory.getDefault().createSocket("example.org", 1234); byte[] data1 = IOUtils.toByteArray(socket1.getInputStream()); socket1.close();/*ww w.ja v a 2 s. c o m*/ assertThat(data1, is(mockData1)); Socket socket2 = SocketFactory.getDefault().createSocket("example.org", 1234); byte[] data2 = IOUtils.toByteArray(socket2.getInputStream()); socket2.close(); assertThat(data2, is(mockData2)); }
From source file:br.ufc.mdcc.mpos.net.util.DiscoveryServiceTcpServer.java
@Override public void clientRequest(Socket connection) throws IOException { OutputStream output = connection.getOutputStream(); InputStream input = connection.getInputStream(); byte dataBuffer[] = new byte[BUFFER]; // PS: in this case "-1" mean a close socket in client side int read = 0; while ((read = input.read(dataBuffer)) != -1) { // e.g.: mpos_serv_req_droid:Calculator:5.0.1 String mposServiceResquest = new String(dataBuffer, 0, read).trim().toLowerCase(); if (mposServiceResquest.startsWith("mpos_serv_req_droid")) { mposServiceReply(output, mposServiceResquest, "android"); } else if (mposServiceResquest.startsWith("mpos_serv_req_wp")) { mposServiceReply(output, mposServiceResquest, "wp"); }/*from w ww. j av a2 s . c o m*/ } close(input); close(output); }
From source file:flens.input.SocketInput.java
@Override public Pair<String, DataInputStream> getStream(Socket newSocket) throws IOException { String hostname = newSocket.getInetAddress().getHostName(); return Pair.of(hostname, new DataInputStream(newSocket.getInputStream())); }
From source file:SmtpTalk.java
/** * Constructor taking a server hostname as argument. *///from w w w .ja va 2 s . c o m SmtpTalk(String server) throws Exception { host = server; try { Socket s = new Socket(host, 25); is = new BufferedReader(new InputStreamReader(s.getInputStream())); os = new PrintStream(s.getOutputStream()); } catch (NoRouteToHostException e) { die("No route to host " + host); } catch (ConnectException e) { die("Connection Refused by " + host); } catch (UnknownHostException e) { die("Unknown host " + host); } catch (IOException e) { die("I/O error setting up socket streams\n" + e); } }
From source file:me.mast3rplan.phantombot.HTTPResponse.java
HTTPResponse(String address, String request) throws IOException { Socket sock = new Socket(address, 80); Writer output = new StringWriter(); IOUtils.write(request, sock.getOutputStream(), "utf-8"); IOUtils.copy(sock.getInputStream(), output); com.gmt2001.Console.out.println(output.toString()); Scanner scan = new Scanner(sock.getInputStream()); String errorLine = scan.nextLine(); for (String line = scan.nextLine(); !line.equals(""); line = scan.nextLine()) { String[] keyval = line.split(":", 2); if (keyval.length == 2) { values.put(keyval[0], keyval[1].trim()); } else {/*from w w w . ja v a2 s .c om*/ //? } } while (scan.hasNextLine()) { body += scan.nextLine(); } sock.close(); }
From source file:com.googlecode.jsendnsca.NagiosPassiveCheckSender.java
public void send(MessagePayload payload) throws NagiosException, IOException { Validate.notNull(payload, "payload cannot be null"); Socket socket = connectedToNagios(); OutputStream outputStream = socket.getOutputStream(); InputStream inputStream = socket.getInputStream(); try {/* w w w . j av a2s . co m*/ outputStream.write(passiveCheck(payload, new DataInputStream(inputStream))); outputStream.flush(); } catch (SocketTimeoutException ste) { throw ste; } catch (IOException e) { throw new NagiosException("Error occurred while sending passive alert", e); } finally { close(socket, outputStream, inputStream); } }