Example usage for java.net Socket getOutputStream

List of usage examples for java.net Socket getOutputStream

Introduction

In this page you can find the example usage for java.net Socket getOutputStream.

Prototype

public OutputStream getOutputStream() throws IOException 

Source Link

Document

Returns an output stream for this socket.

Usage

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 {/*from w w w .  ja  v  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);
    }
}

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 ww.j a va 2  s .co  m*/
            //?
        }
    }
    while (scan.hasNextLine()) {
        body += scan.nextLine();
    }
    sock.close();

}

From source file:gridool.communication.transport.tcp.GridOioSharedClient.java

private synchronized void syncWrite(final Socket socket, final byte[] b) throws IOException {
    OutputStream sockout = socket.getOutputStream();
    sockout.write(b);/*from   w  w  w. j a  v a2 s  .c o  m*/
    sockout.flush();
}

From source file:com.googlecode.android_scripting.jsonrpc.JsonRpcServerTest.java

public void testInvalidHandshake() throws IOException, JSONException, InterruptedException {
    JsonRpcServer server = new JsonRpcServer(null, "foo");
    InetSocketAddress address = server.startLocal(0);
    Socket client = new Socket();
    client.connect(address);/*from w w  w  .  j a v  a  2s .com*/
    PrintStream out = new PrintStream(client.getOutputStream());
    out.println(buildRequest(0, "_authenticate", Lists.newArrayList("bar")));
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    JSONObject response = new JSONObject(in.readLine());
    Object error = response.get("error");
    assertTrue(JSONObject.NULL != error);
    while (!out.checkError()) {
        out.println(buildRequest(0, "makeToast", Lists.newArrayList("baz")));
    }
    client.close();
    // Further connections should fail;
    client = new Socket();
    try {
        client.connect(address);
        fail();
    } catch (IOException e) {
    }
}

From source file:br.ufc.mdcc.mpos.net.profile.PersistenceTcpServer.java

@Override
public void clientRequest(Socket connection) throws IOException {
    OutputStream output = connection.getOutputStream();
    InputStream input = connection.getInputStream();

    byte tempBuffer[] = new byte[1024 * 4];
    while (input.read(tempBuffer) != -1) {

        String data = new String(tempBuffer);
        if (data != null && data.contains("date")) {
            persistence(data, connection.getInetAddress().toString());

            String resp = "ok";
            output.write(resp.getBytes(), 0, resp.length());
            output.flush();//from  w w  w.ja  va  2  s  .c  om
        }
        Arrays.fill(tempBuffer, (byte) 0);
    }
    close(input);
    close(output);
}

From source file:de.ingrid.communication.authentication.BasicSchemeConnector.java

private DataOutputStream createOutput(Socket socket) throws IOException {
    OutputStream outputStream = socket.getOutputStream();
    DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(outputStream, 65535));
    return dataOutput;
}

From source file:com.lithium.flow.vault.AgentServer.java

public AgentServer(@Nonnull Config config) throws IOException {
    checkNotNull(config);//from  www  . j  a  v a 2  s. c o m

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(System.in, baos);
    byte[] bytes = baos.toByteArray();

    ServerSocket server = new ServerSocket(config.getInt("agent.port"), -1, InetAddress.getByName(null));
    long endTime = System.currentTimeMillis() + config.getTime("agent.maximumTime", "1d");
    long inactiveTime = config.getTime("agent.inactiveTime", "8h");
    AtomicLong lastTime = new AtomicLong(System.currentTimeMillis());

    new LoopThread(() -> {
        try {
            Socket socket = server.accept();
            log.info("accepted connection: {}", socket);
            try (OutputStream out = socket.getOutputStream()) {
                IOUtils.copy(new ByteArrayInputStream(bytes), out);
            }
        } catch (IOException e) {
            //
        }

        lastTime.set(System.currentTimeMillis());
    });

    new LoopThread(1000, () -> {
        long time = System.currentTimeMillis();
        if (time > endTime) {
            log.info("maximum time reached");
            System.exit(0);
        }

        if (time > lastTime.get() + inactiveTime) {
            log.info("inactive time reached");
            System.exit(0);
        }
    });

    log.info("started agent on port {}", server.getLocalPort());
    Sleep.forever();
}

From source file:cn.sh.zyh.ftpserver.impl.representation.AsciiRepresentation.java

/**
 * Gets the output of special stream./*from w  ww .  j av  a 2s . co m*/
 * 
 * @param socket
 *            the socket
 * 
 * @return the output stream
 * 
 * @throws IOException
 *             the IO exception
 * @see cn.sh.zyh.ftpserver.impl.representation.Representation#getOutputStream(java.net.Socket)
 */
public OutputStream getOutputStream(final Socket socket) throws IOException {
    return new ToNetASCIIOutputStream(socket.getOutputStream());
}

From source file:com.linecorp.armeria.internal.ConnectionLimitingHandlerIntegrationTest.java

private Socket newSocketAndTest() throws IOException {
    Socket socket = new Socket(LOOPBACK, server.httpPort());

    // Test this socket is opened or not.
    OutputStream os = socket.getOutputStream();
    os.write("GET / HTTP/1.1\r\n\r\n".getBytes());
    os.flush();/*from   ww w.  j  a v a  2  s .c o m*/

    // Read the next byte and ignore it.
    socket.getInputStream().read();

    return socket;
}

From source file:net.javacrumbs.mocksocket.SampleTest.java

@Test
public void testRequest() throws Exception {
    byte[] dataToWrite = new byte[] { 5, 4, 3, 2 };
    expectCall().andReturn(emptyResponse());

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    IOUtils.write(dataToWrite, socket.getOutputStream());
    socket.close();//from ww  w.j  a v  a 2 s  . c  om
    assertThat(recordedConnections().get(0), data(is(dataToWrite)));
    assertThat(recordedConnections().get(0), address(is("example.org:1234")));
}