Example usage for java.net Socket getInputStream

List of usage examples for java.net Socket getInputStream

Introduction

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

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Returns an input stream for this socket.

Usage

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);// ww  w  . j a  va  2 s .c  om
    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:com.mediaportal.ampdroid.activities.videoplayback.StreamProxy.java

private HttpRequest readRequest(Socket client) {
    HttpRequest request = null;//from  ww  w  .j  a  v  a  2 s .c o  m
    InputStream is;
    String firstLine;
    try {
        is = client.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192);
        firstLine = reader.readLine();
    } catch (IOException e) {
        Log.e(LOG_TAG, "Error parsing request", e);
        return request;
    }

    if (firstLine == null) {
        Log.i(LOG_TAG, "Proxy client closed connection without a request.");
        return request;
    }

    StringTokenizer st = new StringTokenizer(firstLine);
    String method = st.nextToken();
    //String uri = st.nextToken();
    //    Log.d(LOG_TAG, uri);
    //    String realUri = uri.substring(1);
    //    Log.d(LOG_TAG, realUri);
    request = new BasicHttpRequest(method, mUrl);
    return request;
}

From source file:any.Connection.java

public Connection(String name, Linker service, Socket socket) throws InterruptedException, IOException {
    this.service = service;

    out = socket.getOutputStream();/* w w  w .j a  v a 2s  .  co m*/
    in = socket.getInputStream();

    // br.mark(256);
    Thread.sleep(100);

    outThread = new OutThread(out, this);
    inThread = new InThread(in, this);

    outThread.setName(outThread.getName() + " " + name + " (out con)");
    inThread.setName(inThread.getName() + " " + name + "  (in con)");

    inThread.start();
    outThread.start();
    // setup and negotiation, send available protocols first

    logger.info("+SERVICE: new connection established with name " + name);
}

From source file:dualcontrol.CryptoClientDemo.java

private void call(Properties properties, MockableConsole console, String hostAddress, int port, byte[] data)
        throws Exception {
    Socket socket = SSLContexts.create(false, "cryptoclient.ssl", properties, console).getSocketFactory()
            .createSocket(hostAddress, port);
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    dos.writeShort(data.length);/*w  ww .j  a  va2  s  . co m*/
    dos.write(data);
    dos.flush();
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    byte[] ivBytes = new byte[dis.readShort()];
    dis.readFully(ivBytes);
    byte[] bytes = new byte[dis.readShort()];
    dis.readFully(bytes);
    if (new String(data).contains("DECRYPT")) {
        System.err.printf("INFO CryptoClientDemo decrypted %s\n", new String(bytes));
    } else {
        System.out.printf("%s:%s\n", Base64.encodeBase64String(ivBytes), Base64.encodeBase64String(bytes));
    }
    socket.close();
}

From source file:org.accada.hal.impl.sim.SimulatorClient.java

private boolean connect() {
    Socket socket;
    try {/* w w  w  .ja  va 2s.c om*/
        socket = new Socket(host, port);
        out = socket.getOutputStream();
        in = socket.getInputStream();
        unkownHost = false;
        return true;
    } catch (UnknownHostException e) {
        unkownHost = true;
        LOG.info("Unknown host.");
        return false;
    } catch (IOException e) {
        LOG.info("Connection refused.");
        return false;
    }
}

From source file:org.jasig.portlet.cms.model.security.ClamAVAntiVirus.java

private String sendSocket(final Socket socket, final String command) throws Exception {
    String answer = null;/*  ww  w  .j  a  v a 2s .  c o m*/
    BufferedReader reader = null;
    PrintWriter writer = null;

    try {
        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

        writer.println(command);
        writer.flush();

        answer = reader.readLine();
        if (answer != null)
            answer = answer.trim();

    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (final IOException e) {
            logger.error(e.getMessage(), e);
        }

        if (writer != null)
            writer.close();
    }
    return answer;
}

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

@Nullable
private String readAgent() {
    String agentPort = store.getValue("vault.agent.port");
    String agentPassword = store.getValue("vault.agent.password");
    if (agentPort == null || agentPassword == null) {
        return null;
    }/* w  w w.j  a v a2  s.c o m*/

    try {
        int port = Integer.parseInt(agentPort);
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("localhost", port), 5000);

        @SuppressWarnings("unchecked")
        Map<String, String> map = mapper.readValue(socket.getInputStream(), Map.class);

        Store agentStore = new MemoryStore(map);
        Vault agentVault = new SecureVault(Configs.empty(), agentStore);
        agentVault.unlock(agentPassword);
        return agentVault.getValue("password");
    } catch (Exception e) {
        log.debug("failed to read from agent", e);
        return null;
    }
}

From source file:net.lightbody.bmp.proxy.jetty.http.ajp.AJP13Listener.java

/**
 * Create an AJP13Connection instance. This method can be used to override
 * the connection instance.//w ww  .  ja va 2  s .  c o m
 * 
 * @param socket
 *            The underlying socket.
 */
protected AJP13Connection createConnection(Socket socket) throws IOException {
    return new AJP13Connection(this, socket.getInputStream(), socket.getOutputStream(), socket,
            getBufferSize());
}

From source file:com.gargoylesoftware.htmlunit.NoHttpResponseTest.java

@Override
public void run() {
    try {//from   w w  w  .j  a  v  a2  s  . c o m
        serverSocket_ = new ServerSocket(port_);
        started_.set(true);
        LOG.info("Starting listening on port " + port_);
        while (!shutdown_) {
            final Socket s = serverSocket_.accept();
            final BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

            final CharBuffer cb = CharBuffer.allocate(5000);
            br.read(cb);
            cb.flip();
            final String in = cb.toString();
            cb.rewind();

            final RawResponseData responseData = getResponseData(in);

            if (responseData == null || responseData.getStringContent() == DROP_CONNECTION) {
                LOG.info("Closing impolitely in & output streams");
                s.getOutputStream().close();
            } else {
                final PrintWriter pw = new PrintWriter(s.getOutputStream());
                pw.println("HTTP/1.0 " + responseData.getStatusCode() + " " + responseData.getStatusMessage());
                for (final NameValuePair header : responseData.getHeaders()) {
                    pw.println(header.getName() + ": " + header.getValue());
                }
                pw.println();
                pw.println(responseData.getStringContent());
                pw.println();
                pw.flush();
                pw.close();
            }
            br.close();
            s.close();
        }
    } catch (final SocketException e) {
        if (!shutdown_) {
            LOG.error(e);
        }
    } catch (final IOException e) {
        LOG.error(e);
    } finally {
        LOG.info("Finished listening on port " + port_);
    }
}

From source file:pl.edu.agh.ServiceConnection.java

/**
 * 1. Sends data to service: console password | service password | service id
 *///from www .  j a v a 2 s.c  om
public void connect(Service service) {
    try {
        LOGGER.info("Connecting to service: " + service);
        Socket socket = new Socket(service.getHost(), service.getPort());
        socket.setSoTimeout(300);
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        out.writeBytes(service.getPassword() + "|" + service.getId() + "\r\n");
        String response = in.readLine();

        in.close();
        out.close();
        socket.close();

        LOGGER.info("Service response: " + response);

    } catch (Exception e) {
        LOGGER.error("Error connecting with daemon: " + e.getMessage());
    }
}