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: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 ww .ja va  2s  . c  o  m*/
        }
        Arrays.fill(tempBuffer, (byte) 0);
    }
    close(input);
    close(output);
}

From source file:nfinity.FindPeer.java

public Boolean ConnectPeerListener(String ip) {
    try {// w w  w .  j a v a2  s . c  o  m
        //Soocket = socket = new Socket(ip, 1000);
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress(ip, 1000), 1000);
        DataInputStream input = new DataInputStream(socket.getInputStream());
        String data = input.readUTF();
        System.out.println(data);
        String rspdata[] = data.split(" ");
        if (!rspdata[2].equals("")) {
            RemotePeerID = rspdata[2];
            return true;
        }
        socket.close();
    } catch (IOException ex) {
        System.out.println("Connection failed " + ex.getMessage());
    }
    return false;
}

From source file:com.google.dataconnector.util.Rfc1929SdcAuthenticator.java

/**
 * Reads the authentication data from the session and validates request
 * /*from  w  w w  .  j a va 2s.  c  o  m*/
 * @param s connected socket from incoming SOCKS client.
 * @throws IOException if there are any socket communication issues.
 */
@Override
public ServerAuthenticator startSession(final Socket s) throws IOException {
    final InputStream in = s.getInputStream();
    final OutputStream out = s.getOutputStream();

    if (in.read() != 5) {
        // Drop non version 5 messages.
        return null;
    }

    if (!selectSocks5Authentication(in, out, METHOD_ID))
        return null;
    if (!doUserPasswordAuthentication(s, in, out))
        return null;

    return new Rfc1929SdcAuthenticator(in, out, passKey, keyManager, serverMetaData);
}

From source file:Network.Dengue.CDengueHandler.java

@Override
public void update(Observable o, Object arg) {

    int intReturn = 1;

    Socket objSocket = (Socket) arg;

    StringBuilder objSB = new StringBuilder();

    try {/*from   ww  w . ja v a  2 s  .  com*/

        BufferedReader objReader = new BufferedReader(new InputStreamReader(objSocket.getInputStream()));

        objSB.append(objReader.readLine());

        new MDengue().addCluster(objSB.toString());

        CNotificationManager.notifiyDengue();
        CPublisherManager.publishDengue();

    } catch (IOException | ParseException ex) {
        System.out.println(ex);
        intReturn = -1;
    } finally {

        try (Writer objWriter = Channels.newWriter(Channels.newChannel(objSocket.getOutputStream()),
                StandardCharsets.US_ASCII.name())) {

            objWriter.append(intReturn + "");

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

From source file:Network.Haze.CHazeHandler.java

@Override
public void update(Observable o, Object arg) {

    int intReturn = 1;

    Socket objSocket = (Socket) arg;

    StringBuilder objSB = new StringBuilder();

    try {/*from   w w w  .ja  va  2  s  .c o  m*/

        BufferedReader objReader = new BufferedReader(new InputStreamReader(objSocket.getInputStream()));

        objSB.append(objReader.readLine());

        new MHaze().addHazeInfo(objSB.toString());

        CNotificationManager.notifiyHaze();
        CPublisherManager.publishHaze();

    } catch (IOException | ParseException ex) {
        System.out.println(ex);
        intReturn = -1;
    } finally {

        try (Writer objWriter = Channels.newWriter(Channels.newChannel(objSocket.getOutputStream()),
                StandardCharsets.US_ASCII.name())) {

            objWriter.append(intReturn + "");

        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

From source file:flens.input.GraphiteInput.java

@Override
public Pair<String, BufferedReader> getStream(Socket newSocket) throws IOException {
    String hostname = newSocket.getInetAddress().getHostName();
    return Pair.of(hostname, new BufferedReader(new InputStreamReader(newSocket.getInputStream())));
}

From source file:dualcontrol.CryptoHandler.java

public void handle(DualControlKeyStoreSession dualControl, Socket socket) throws Exception {
    try {/*w w w .  j a  v  a2s.  c o  m*/
        this.dualControl = dualControl;
        DataInputStream dis = new DataInputStream(socket.getInputStream());
        int length = dis.readShort();
        byte[] bytes = new byte[length];
        dis.readFully(bytes);
        String data = new String(bytes);
        String[] fields = data.split(":");
        String mode = fields[0];
        String alias = fields[1];
        this.dos = new DataOutputStream(socket.getOutputStream());
        if (mode.equals("GETKEY")) {
            if (enableGetKey) {
                SecretKey key = dualControl.loadKey(alias);
                dos.writeUTF(key.getAlgorithm());
                write(key.getEncoded());
            }
        } else {
            cipher(mode, alias, fields[2], fields[3], fields[4]);
        }
    } finally {
        dos.close();
    }
}

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

@Test
public void testConditionalData() throws Exception {
    byte[] dataToWrite = new byte[] { 5, 4, 3, 2 };
    byte[] mockData = new byte[] { 1, 2, 3, 4 };
    expectCall().andWhenRequest(data(is(dataToWrite))).thenReturn(data(mockData));

    Socket socket = SocketFactory.getDefault().createSocket("example.org", 1234);
    IOUtils.write(dataToWrite, socket.getOutputStream());
    byte[] data = IOUtils.toByteArray(socket.getInputStream());
    socket.close();/*  w w w  .j a  v  a 2 s  . c o m*/
    assertThat(data, is(mockData));
}

From source file:Main.java

public InputStream getWWWPageStream(String host, String file) throws IOException, UnknownHostException {

    InetAddress webServer = InetAddress.getByName(host);

    Socket httpPipe = new Socket(webServer, 80);
    if (httpPipe == null) {
        System.out.println("Socket to Web server creation failed.");
        return null;
    }//from  w  w w .j  a  v a 2s  .co  m

    InputStream inn = httpPipe.getInputStream(); // get raw streams
    OutputStream outt = httpPipe.getOutputStream();

    DataInputStream in = new DataInputStream(inn); // turn into higher-level ones
    PrintStream out = new PrintStream(outt);

    if (inn == null || outt == null) {
        System.out.println("Failed to open streams to socket.");
        return null;
    }
    out.println("GET " + file + " HTTP/1.0\n");

    String response;
    while ((response = in.readUTF()).length() > 0) {
        System.out.println(response);
    }

    return in;
}

From source file:InterruptibleSocketTest.java

/**
 * Connects to the test server, using blocking I/O
 *//* ww  w  .ja  va2s.com*/
public void connectBlocking() throws IOException {
    messages.append("Blocking:\n");
    Socket sock = new Socket("localhost", 8189);
    try {
        in = new Scanner(sock.getInputStream());
        while (!Thread.currentThread().isInterrupted()) {
            messages.append("Reading ");
            if (in.hasNextLine()) {
                String line = in.nextLine();
                messages.append(line);
                messages.append("\n");
            }
        }
    } finally {
        sock.close();
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                messages.append("Socket closed\n");
                interruptibleButton.setEnabled(true);
                blockingButton.setEnabled(true);
            }
        });
    }
}