Example usage for java.io DataInputStream DataInputStream

List of usage examples for java.io DataInputStream DataInputStream

Introduction

In this page you can find the example usage for java.io DataInputStream DataInputStream.

Prototype

public DataInputStream(InputStream in) 

Source Link

Document

Creates a DataInputStream that uses the specified underlying InputStream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://localhost:8080/postedresults.jsp");
    URLConnection conn = url.openConnection();
    conn.setDoInput(true);//ww  w . j  a  v a2s.  co  m
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    String content = "CONTENT=HELLO JSP !&ONEMORECONTENT =HELLO POST !";

    out.writeBytes(content);
    out.flush();
    out.close();

    DataInputStream in = new DataInputStream(conn.getInputStream());
    String str;
    while (null != ((str = in.readUTF()))) {
        System.out.println(str);
    }
    in.close();
}

From source file:finger.java

public static void main(String[] args) {

    String hostname;/*from  w  w  w  .j  a  v a 2 s .  c  om*/
    Socket theSocket;
    DataInputStream theFingerStream;
    PrintStream ps;

    try {
        hostname = args[0];
    } catch (Exception e) {
        hostname = "localhost";
    }

    try {
        theSocket = new Socket(hostname, port, true);
        ps = new PrintStream(theSocket.getOutputStream());
        for (int i = 1; i < args.length; i++)
            ps.print(args[i] + " ");
        ps.print("\r\n");
        theFingerStream = new DataInputStream(theSocket.getInputStream());
        String s;
        while ((s = theFingerStream.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String query = "name=yourname&email=youremail@yourserver.com";

    URLConnection uc = new URL("http:// your form ").openConnection();
    uc.setDoOutput(true);//from  w w w.j  av a 2s  .  c o m
    uc.setDoInput(true);
    uc.setAllowUserInteraction(false);
    DataOutputStream dos = new DataOutputStream(uc.getOutputStream());

    // The POST line, the Accept line, and
    // the content-type headers are sent by the URLConnection.
    // We just need to send the data
    dos.writeBytes(query);
    dos.close();

    // Read the response
    DataInputStream dis = new DataInputStream(uc.getInputStream());
    String nextline;
    while ((nextline = dis.readLine()) != null) {
        System.out.println(nextline);
    }
    dis.close();
}

From source file:delete_tcp.java

public static void main(String ar[]) throws IOException {
    ServerSocket ss = new ServerSocket(9999);

    Socket s = ss.accept();/*from  w  w w  .j  ava 2s .  co m*/

    DataInputStream in = new DataInputStream(s.getInputStream());
    DataOutputStream out = new DataOutputStream(s.getOutputStream());

    String id = in.readUTF();

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    EmployeeJDBCTemplate employeeJDBCTemplate = (EmployeeJDBCTemplate) context.getBean("employeeJDBCTemplate");

    System.out.println("Deleting Records...");

    employeeJDBCTemplate.delete(Integer.parseInt(id));

    out.writeUTF("Success");

    s.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] b = new byte[1];
    Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost", "proxy.mydomain.local");
    systemSettings.put("http.proxyPort", "80");

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    BASE64Encoder encoder = new BASE64Encoder();
    String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes());
    con.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);
    DataInputStream di = new DataInputStream(con.getInputStream());
    while (-1 != di.read(b, 0, 1)) {
        System.out.print(new String(b));
    }/*from  w w w  .j a  v a 2  s . co m*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    byte[] b = new byte[1];
    Properties systemSettings = System.getProperties();
    systemSettings.put("http.proxyHost", "proxy.mydomain.local");
    systemSettings.put("http.proxyPort", "80");

    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("mydomain\\username", "password".toCharArray());
        }/*from w  w w  .j a v a2 s . co  m*/
    });

    URL u = new URL("http://www.google.com");
    HttpURLConnection con = (HttpURLConnection) u.openConnection();
    DataInputStream di = new DataInputStream(con.getInputStream());
    while (-1 != di.read(b, 0, 1)) {
        System.out.print(new String(b));
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];/*from   ww w. j a v  a 2  s  .c  om*/
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String sessionCookie = null;/*from   ww w. ja  v  a 2  s. c om*/
    URL url = new java.net.URL("http://127.0.0.1/yourServlet");
    URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    System.out.println(count);
}

From source file:cacheservice.CacheServer.java

/**
 * @param args the command line arguments
 *//*w ww.  j av  a 2  s  . co m*/
public static void main(String[] args) {
    // COMUNICACIN CON EL CLIENTE
    ServerSocket serverSocket;
    Socket socketCliente;
    DataInputStream in; //Flujo de datos de entrada
    DataOutputStream out; //Flujo de datos de salida
    String mensaje;
    int laTengoenCache = 0;

    //COMUNICACIN CON EL INDEX
    ServerSocket serverSocketIndex;
    Socket socketIndex;
    DataOutputStream outIndex;
    ObjectInputStream inIndex;
    String mensajeIndex;

    try {
        serverSocket = new ServerSocket(4444);
        System.out.print("SERVIDOR CACHE ACTIVO a la espera de peticiones");

        //MIENTRAS PERMANEZCA ACTIVO EL SERVIDOR CACHE ESPERAR? POR PETICIONES DE LOS CLIENTES
        while (true) {
            socketCliente = serverSocket.accept();
            in = new DataInputStream(socketCliente.getInputStream()); //Entrada de los mensajes del cliente
            mensaje = in.readUTF(); //Leo el mensaje enviado por el cliente

            System.out.println("\nHe recibido del cliente: " + mensaje); //Muestro el mensaje recibido por el cliente
            //int particionBuscada = seleccionarParticion(mensaje, tamanoCache, numeroParticiones); //Busco la particin
            //double tamanoParticion = Math.ceil( (double)tamanoCache / (double)numeroParticiones);

            //Thread hilo = new Hilo(mensaje,particionBuscada,cache.GetTable(),(int) tamanoParticion);
            //hilo.start();

            //RESPUESTA DEL SERVIDOR CACHE AL CLIENTE
            out = new DataOutputStream(socketCliente.getOutputStream());
            String respuesta = "Respuesta para " + mensaje;
            if (laTengoenCache == 1) {
                out.writeUTF(respuesta);
                System.out.println("\nTengo la respuesta. He respondido al cliente: " + respuesta);
            } else {
                out.writeUTF("miss");
                out.close();
                in.close();
                socketCliente.close();

                System.out.println("\nNo tengo la respuesta.");

                //LEER RESPUESTA DEL SERVIDOR INDEX
                serverSocketIndex = new ServerSocket(6666);
                socketIndex = serverSocketIndex.accept();
                inIndex = new ObjectInputStream(socketIndex.getInputStream());
                JSONObject mensajeRecibidoIndex = (JSONObject) inIndex.readObject();

                System.out.println("He recibido del SERVIDOR INDEX: " + mensajeRecibidoIndex);

                //outIndex.close();
                inIndex.close();
                socketIndex.close();

            }

        }
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }
}

From source file:com.ccc.nhr.test1.NhrSocket.java

public static void main(String args[]) throws IOException, SQLException, ClassNotFoundException {

    final String host = "192.168.16.146";
    final int portNumber = 10010;

    System.out.println("Creating socket to '" + host + "' on port " + portNumber);
    Socket socket = new Socket(host, portNumber);

    final NhrDataService nhrConnection = new NhrConnectionBuilder(socket)
            .withInputBufferedReader(new BufferedReader(new InputStreamReader(socket.getInputStream())))
            .withDataInputStream(new DataInputStream(socket.getInputStream()))
            .withDataOutputStream(new DataOutputStream(socket.getOutputStream())).build();

    ReceiverThread receiverThread = new ReceiverThread();
    receiverThread.setNhrConnection(nhrConnection);
    receiverThread.start();//from   ww  w  .j  a v a 2  s .c  o  m

    SenderThread senderThread = new SenderThread();
    senderThread.setNhrConnection(nhrConnection);
    senderThread.start();

}