List of usage examples for java.net Socket getOutputStream
public OutputStream getOutputStream() throws IOException
From source file:com.davidcode.code.util.ErrorBuffer.java
/** * Writes the data to a socket using prehistoric java * * @param socket//from w w w . j a va2 s . c o m * @throws IOException */ public void write(Socket socket) throws IOException { //Get the data in byte form byte[] outputBuf = asByteArray(); outputBuf = xor(outputBuf); //Write the data to the socket which is connected to a server for recieving this data DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); dataOutputStream.write(outputBuf); //Flush and close the stream dataOutputStream.flush(); dataOutputStream.close(); }
From source file:com.sshtools.j2ssh.agent.SshAgentSocketListener.java
/** * Starts the agent listener thread//from ww w. j a va2 s .c om */ public void start() { thread = new Thread(new Runnable() { public void run() { try { Socket socket; System.setProperty("sshtools.agent", location); state.setValue(StartStopState.STARTED); while ((socket = server.accept()) != null) { SshAgentConnection agentClient = new SshAgentConnection(keystore, socket.getInputStream(), socket.getOutputStream()); } thread = null; } catch (IOException ex) { log.info("The agent listener closed: " + ex.getMessage()); } finally { state.setValue(StartStopState.STOPPED); } } }); thread.start(); }
From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java
private void query(int port, String key, byte[] expected) throws IOException { final Socket socket = new Socket(InetAddress.getLocalHost(), port); final Writer out = new OutputStreamWriter(socket.getOutputStream()); out.write(key);// ww w . j a va 2 s. c o m out.write('\n'); out.flush(); final InputStream in = socket.getInputStream(); final byte[] buffer = new byte[1024]; in.read(buffer); socket.close(); System.out.println(new String(buffer)); assertEquals('Z', buffer[0]); assertEquals('B', buffer[1]); assertEquals('X', buffer[2]); assertEquals('D', buffer[3]); assertEquals((byte) 0x01, buffer[4]); byte[] actual = Arrays.copyOfRange(buffer, 13, 13 + expected.length); assertArrayEquals(expected, actual); // assertTrue(Arrays.equals(expected, actual)); }
From source file:gobblin.tunnel.ConnectProxyServer.java
@Override void handleClientSocket(Socket clientSocket) throws IOException { final InputStream clientToProxyIn = clientSocket.getInputStream(); BufferedReader clientToProxyReader = new BufferedReader(new InputStreamReader(clientToProxyIn)); final OutputStream clientToProxyOut = clientSocket.getOutputStream(); String line = clientToProxyReader.readLine(); String connectRequest = ""; while (line != null && isServerRunning()) { connectRequest += line + "\r\n"; if (connectRequest.endsWith("\r\n\r\n")) { break; }/* w w w . j ava2 s . co m*/ line = clientToProxyReader.readLine(); } // connect to given host:port Matcher matcher = hostPortPattern.matcher(connectRequest); if (!matcher.find()) { try { sendConnectResponse("400 Bad Request", clientToProxyOut, null, 0); } finally { clientSocket.close(); stopServer(); } return; } String host = matcher.group(1); int port = Integer.decode(matcher.group(2)); // connect to server Socket serverSocket = new Socket(); try { serverSocket.connect(new InetSocketAddress(host, port)); addSocket(serverSocket); byte[] initialServerResponse = null; int nbytes = 0; if (mixServerAndProxyResponse) { // we want to mix the initial server response with the 200 OK initialServerResponse = new byte[64]; nbytes = serverSocket.getInputStream().read(initialServerResponse); } sendConnectResponse("200 OK", clientToProxyOut, initialServerResponse, nbytes); } catch (IOException e) { try { sendConnectResponse("404 Not Found", clientToProxyOut, null, 0); } finally { clientSocket.close(); stopServer(); } return; } final InputStream proxyToServerIn = serverSocket.getInputStream(); final OutputStream proxyToServerOut = serverSocket.getOutputStream(); _threads.add(new EasyThread() { @Override void runQuietly() throws Exception { try { IOUtils.copy(clientToProxyIn, proxyToServerOut); } catch (IOException e) { LOG.warn("Exception " + e.getMessage() + " on " + getServerSocketPort()); } } }.startThread()); try { if (nBytesToCloseSocketAfter > 0) { // Simulate proxy abruptly closing connection int leftToRead = nBytesToCloseSocketAfter; byte[] buffer = new byte[leftToRead + 256]; while (true) { int numRead = proxyToServerIn.read(buffer, 0, leftToRead); if (numRead < 0) { break; } clientToProxyOut.write(buffer, 0, numRead); clientToProxyOut.flush(); leftToRead -= numRead; if (leftToRead <= 0) { LOG.warn("Cutting connection after " + nBytesToCloseSocketAfter + " bytes"); break; } } } else { IOUtils.copy(proxyToServerIn, clientToProxyOut); } } catch (IOException e) { LOG.warn("Exception " + e.getMessage() + " on " + getServerSocketPort()); } clientSocket.close(); serverSocket.close(); }
From source file:info.varden.irclinqed.dcc.FileReceiveThread.java
@Override public void run() { try {/* ww w . j a va2 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:org.openmrs.module.pacsintegration.component.HL7ListenerComponentTest.java
@Test public void shouldNotCreateDuplicateReport() throws Exception { ModuleActivator activator = new PacsIntegrationActivator(); activator.started();/*from www . j a v a 2s .co m*/ Context.getService(PacsIntegrationService.class).initializeHL7Listener(); List<Patient> patients = patientService.getPatients(null, "101-6", Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()), true); assertThat(patients.size(), is(1)); // sanity check Patient patient = patients.get(0); List<Encounter> encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(0)); // sanity check try { String message = "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r" + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r" + "PV1|1||||||||||||||||||\r" + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170556||||||||||||MBL^CR||||||F|||||||Test&Goodrich&Mark&&&&||||20130228170556\r" + "OBX|1|TX|127689^SOME_X-RAY||Clinical Indication: ||||||F\r"; Thread.sleep(2000); // give the simple server time to start Socket socket = new Socket("127.0.0.1", 6665); PrintStream writer = new PrintStream(socket.getOutputStream()); for (int i = 0; i < 2; i++) { writer.print(header); writer.print(message); writer.print(trailer + "\r"); writer.flush(); } Thread.sleep(2000); encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(1)); assertThat(encounters.get(0).getObs().size(), is(4)); } finally { activator.stopped(); } }
From source file:SendMail.java
/** * Called when the send button is clicked. Actually sends the mail message. * /* w ww . ja v a 2 s . c o m*/ * @param event * The event. */ void Send_actionPerformed(java.awt.event.ActionEvent event) { try { java.net.Socket s = new java.net.Socket(_smtp.getText(), 25); _out = new java.io.PrintWriter(s.getOutputStream()); _in = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream())); send(null); send("HELO " + java.net.InetAddress.getLocalHost().getHostName()); send("MAIL FROM: " + _from.getText()); send("RCPT TO: " + _to.getText()); send("DATA"); _out.println("Subject:" + _subject.getText()); _out.println(_body.getText()); send("."); s.close(); } catch (Exception e) { _model.addElement("Error: " + e); } }
From source file:org.openmrs.module.pacsintegration.component.HL7ListenerComponentTest.java
@Test public void shouldListenForAndParseORU_R01Message() throws Exception { ModuleActivator activator = new PacsIntegrationActivator(); activator.started();/* ww w. j av a 2 s . com*/ Context.getService(PacsIntegrationService.class).initializeHL7Listener(); List<Patient> patients = patientService.getPatients(null, "101-6", Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()), true); assertThat(patients.size(), is(1)); // sanity check Patient patient = patients.get(0); List<Encounter> encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(0)); // sanity check try { String message = "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r" + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r" + "PV1|1||||||||||||||||||\r" + "OBR|1||0000001297|127689^SOME_X-RAY|||20130228170556||||||||||||MBL^CR||||||F|||||||M123&Goodrich&Mark&&&&||||20130228170556\r" + "OBX|1|TX|127689^SOME_X-RAY||Clinical Indication: ||||||F\r"; Thread.sleep(2000); // give the simple server time to start Socket socket = new Socket("127.0.0.1", 6665); PrintStream writer = new PrintStream(socket.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer.print(header); writer.print(message); writer.print(trailer + "\r"); writer.flush(); Thread.sleep(2000); // confirm that report encounter has been created and has obs (we more thoroughly test the handler in the ORU_R01 handler and Radiology Service (in emr module) tests) encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(1)); assertThat(encounters.get(0).getObs().size(), is(4)); // confirm that the proper ack is sent out String response = reader.readLine(); assertThat(response, containsString("|ACK|")); } finally { activator.stopped(); } }
From source file:org.openmrs.module.pacsintegration.component.HL7ListenerComponentTest.java
@Test public void shouldListenForAndParseORU_R01MessageMissingProcedureCode() throws Exception { ModuleActivator activator = new PacsIntegrationActivator(); activator.started();//w w w .ja v a 2 s .co m Context.getService(PacsIntegrationService.class).initializeHL7Listener(); List<Patient> patients = patientService.getPatients(null, "101-6", Collections.singletonList(emrApiProperties.getPrimaryIdentifierType()), true); assertThat(patients.size(), is(1)); // sanity check Patient patient = patients.get(0); List<Encounter> encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(0)); // sanity check try { String message = "MSH|^~\\&|HMI|Mirebalais Hospital|RAD|REPORTS|20130228174549||ORU^R01|RTS01CE16055AAF5290|P|2.3|\r" + "PID|1||101-6||Patient^Test^||19770222|M||||||||||\r" + "PV1|1||||||||||||||||||\r" + "OBR|1||0000001297||||20130228170556||||||||||||MBL^CR||||||F|||||||M123&Goodrich&Mark&&&&||||20130228170556\r" + "OBX|1|TX|127689^SOME_X-RAY||Clinical Indication: ||||||F\r"; Thread.sleep(2000); // give the simple server time to start Socket socket = new Socket("127.0.0.1", 6665); PrintStream writer = new PrintStream(socket.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); writer.print(header); writer.print(message); writer.print(trailer + "\r"); writer.flush(); Thread.sleep(2000); // confirm that report encounter has been created and has obs (we more thoroughly test the handler in the ORU_R01 handler and Radiology Service (in emr module) tests) encounters = encounterService.getEncounters(patient, null, null, null, null, Collections.singletonList(radiologyProperties.getRadiologyReportEncounterType()), null, null, null, false); assertThat(encounters.size(), is(1)); assertThat(encounters.get(0).getObs().size(), is(3)); // only 3 because no procedure obs // confirm that the proper ack is sent out String response = reader.readLine(); assertThat(response, containsString("|ACK|")); } finally { activator.stopped(); } }
From source file:net.lightbody.bmp.proxy.jetty.http.SocketListener.java
/** Create an HttpConnection instance. This method can be used to * override the connection instance.//www . j a va 2 s.c o m * @param socket The underlying socket. */ protected HttpConnection createConnection(Socket socket) throws IOException { HttpConnection c = new HttpConnection(this, socket.getInetAddress(), socket.getInputStream(), socket.getOutputStream(), socket); return c; }