Simple simulator HTTP Server and Browser(Java)

SimulatorTest.java
~~~~~~~~~~~~~~~~~~

package com.anil.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *  Simulator of HTTP server
 *
 *
 */
public class SimulatorTest implements Runnable {

    private Socket socket;

    /**
     * construct method, create server socket, accept at 8888 port
     */
    public SimulatorTest() {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            socket = serverSocket.accept();

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

        /*
         * create new thread and it
         */
        new Thread(this).start();
    }

    public void run() {
        try {

            /*
             * get input information from browser, and output get request
             * information
             */
            InputStream inputStream = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            char[] buf = new char[512];
            int count = 0;

            System.out.println("---------start-------------");
            while ((count = reader.read(buf)) != -1) {
                for (int i = 0; i < count; i++) {
                    System.out.print(buf[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("---------end-------------");
    }

    /**
     * main method
     */
    public static void main(String[] args) {
        new SimulatorTest();
    }
}



BrowserTest.java
~~~~~~~~~~~~~~~

package com.anil.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * Simulator browser request HTTP
 *
 *
 */
public class BrowserTest implements Runnable {
    private Socket socket;

    public BrowserTest() {
        try {

            /*
             * create new socket, connect to web site
             */
            socket = new Socket("www.google.com", 80);

            /*
             * simulator browser request HTTP information
             */
            OutputStream outputStream = socket.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream);
            writer.println("GET / HTTP/1.1");
            writer.println("Connection: Keep-Alive");
            writer.println();
            writer.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread(this).start();
    }

    public void run() {

        try {

            /*
             * get HTTP response from web site, and output get information
             */
            InputStream inputStream = socket.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            char[] buf = new char[512];
            int count = 0;
            System.out.println("---------start-------------");
            while ((count = reader.read(buf)) != -1) {
                for (int i = 0; i < count; i++) {
                    System.out.print(buf[i]);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("---------end-------------");
    }

    /**
     * main method
     */
    public static void main(String[] args) {
        new BrowserTest();
    }
}

No comments:

Post a Comment