0

This code should be able to respond to client browser.

This is my code for listening to browser:

public class simpleServer extends Thread {
static ServerSocket serverSocket;
public static void main(String[] args) throws IOException {
    serverSocket = new ServerSocket(8000);
    Thread t = new simpleServer();
    t.start();
}

@Override
public void run() {
    while (true) {
        try (Socket socket = serverSocket.accept();
                DataInputStream in = new DataInputStream(socket.getInputStream());) {
            String received = in.readUTF();
            System.out.println("What received: " + received);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
}

Running code result:

java.io.EOFException
at java.io.DataInputStream.readFully(DataInputStream.java:197)
at java.io.DataInputStream.readUTF(DataInputStream.java:609)
at java.io.DataInputStream.readUTF(DataInputStream.java:564)
at myp.simpleServer.run(simpleServer.java:23)

I try localhost:8000

But the unable to connect!

enter image description here

Sachin Janani
  • 1,310
  • 1
  • 17
  • 33
  • Your code has to be hosted on a web server before you'll be able to connect to it using HTTP. – IAmTheSquidward Aug 12 '14 at 17:25
  • @IAmTheSquidward this code is supposed to emulate the web server. – Luiggi Mendoza Aug 12 '14 at 17:27
  • OP: you may start reading all the request as `String`s and printing it out in the Java console. Probably you will get this result in your browser since there's no response from the server but you should see a message printed in console that executes the Java application. Your current problem is that you probably aren't receiving `"http://localhost:8000"` as first message from client, and also you're comparing `String`s with `==` when you should use `equals` method. – Luiggi Mendoza Aug 12 '14 at 17:29
  • @LuiggiMendoza i corrected String comparing. –  Aug 12 '14 at 17:31
  • @LuiggiMendoza oh, I gotcha. My bad. – IAmTheSquidward Aug 12 '14 at 17:31
  • @user3808021 you should add an `else` statement as well... – Luiggi Mendoza Aug 12 '14 at 17:32

2 Answers2

0

I tried this code and works as expected:

public class SimpleServer implements Runnable {
    static ServerSocket serverSocket;
    public static void main(String[] args) throws IOException {
        serverSocket = new ServerSocket(8000);
        Thread t = new Thread(new SimpleServer());
        t.start();
    }

    @Override
    public void run() {
        while (true) {
            try (Socket socket = serverSocket.accept();
                DataInputStream in = new DataInputStream(socket.getInputStream());
                //this is too much noise
                //you can directly wrap the result from socket.getInputStream()
                //into the BufferedReader
                BufferedReader br = new BufferedReader( new InputStreamReader(in) )) {

                System.out.println("request from browser client:");
                String line;
                //reads and prints every line from the request
                while ( (line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}

Now you just need to write an appropriate response for your client.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • `GET / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive` –  Aug 12 '14 at 17:59
0

Whenever you fire a URL from browser it by default calls get method with a header something like this

Got connection
GET / HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

So your condition if (in.readUTF() == "http://yourhost:8000") will never be satisfied.Going forward with your error.The server as per your code the server will always be in reading mode because of this statement if (in.readUTF() == "http://yourhost:8000") so it will not send any response to you browser.To help you to understand the how it works I have modified your code as follows:

@Override
public void run() {

        try (Socket socket = serverSocket.accept();
                DataInputStream in = new DataInputStream(socket.getInputStream());) {
            System.out.println("Got connection");
            String line="";
            while(null!=(line=in.readLine())){
            System.out.println(line);
            }
           // if (in.readUTF() == "http://yourhostname:8000") {
             //   System.out.println("Browser want local host");
           // }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

}

Hope this answer your question.

Sachin Janani
  • 1,310
  • 1
  • 17
  • 33
  • REsult is : `GET / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive` –  Aug 12 '14 at 18:05
  • But, cant respond to browser request, why? –  Aug 12 '14 at 18:05
  • As server is always reading data from browser and is waiting for data from browser.I think you need to write a separate thread that will read data from the socket. – Sachin Janani Aug 12 '14 at 18:08