Java对浏览器的响应

时间:2014-08-12 17:23:19

标签: java io serversocket

此代码应该能够响应客户端浏览器。

这是我收听浏览器的代码:

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();
        }
    }
}
}

运行代码结果:

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)

我尝试localhost:8000

但无法连接!

enter image description here

2 个答案:

答案 0 :(得分:0)

我尝试了这段代码并按预期工作:

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();
            }
        }
    }
}

现在您只需要为您的客户撰写适当的回复。

更多信息:

答案 1 :(得分:0)

每当您从浏览器触发URL时,默认情况下都会调用带有类似此标题的get方法

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

所以你的条件if(in.readUTF()==“http:// yourhost:8000”)将永远不会满意。向前看你的错误。服务器根据你的代码,服务器将始终处于阅读模式因为这句话if(in.readUTF()==“http:// yourhost:8000”)所以它不会向你的浏览器发送任何回复。为了帮助你理解它是如何工作的,我修改你的代码如下:

@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();
        }

}

希望这能回答你的问题。