2

所以我们在课堂上玩弄 ServerSockets,制作一个非常简单的 HTTP 服务器,它接受一个请求,不做任何事情,并以 200 OK 响应,然后是一些 HTML 内容。

这两天我一直在想办法解决这个问题,我一直没能掌握,我的老师也没有。由于某些奇怪的原因,我开始认为关闭服务器是一个问题。我已经解决了这个问题,但只想知道为什么我首先会发生这种情况。

以下是三个片段:

HttpServer.class:

package httpserver;

import java.io.Closeable;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class HttpServer implements Closeable {
    public static final int PORT = 80;
    public static final int BACKLOG = 1;
    public static final String ROOT_CATALOG = "C:/HttpServer/";

    private ServerSocket server;
    private Socket client;
    private Scanner in;
    private PrintWriter out;

    private String request;

    public HttpServer() throws IOException {
        server = new ServerSocket(PORT, BACKLOG);
    }

    public Socket accept() throws IOException {
        client = server.accept();
        in = new Scanner(client.getInputStream());
        out = new PrintWriter(client.getOutputStream());

        return client;
    }

    public void recieve() {
        request = in.nextLine();
        System.out.println(request);
    }

    public void respond(final String message) {
        out.print(message);
        out.flush();
    }

    @Override
    public void close() throws IOException {
        if(!server.isClosed()) {
            client = null;
            server = null;
        }
    }
}

有效的 Main.class 解决方案

package httpserver;

import java.io.IOException;
import java.net.Socket;

public class Main {
    public static void main(String[] args) throws IOException {
        HttpServer server = new HttpServer();
        Socket client;

        while(true) {
            client = server.accept();
            server.recieve();
            server.respond("HTTP/1.0 200 OK\r\n"
                    + "Content-Type: text/html\r\n"
                    + "\r\n"
                    + "<html><body><b>hello..</b></body></html>");
            client.close();
        }
    }
}

不起作用的 Main.class 解决方案:

package httpserver;

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try(HttpServer server = new HttpServer()) {
            while (true) {
                server.accept();
                server.recieve();
                server.respond("HTTP/1.0 200 OK\r\n"
                        + "Content-Type: text/html\r\n"
                        + "\r\n"
                        + "<html><body><b>hello..</b></body></html>");
            }
        } catch(IOException ex) {
            System.out.println("We have a problem: " + ex.getMessage());
        }
    }
}

I could imagine it has something to do with not closing the client socket after each loop iteration. But even so, it should at least go through once, before bugging up in that case. I really can't see what the problem is supposed to be.

No error messages, nothing...

4

1 回答 1

1

You do not specify any Content-length when sending the HTTP, so the browser does not know when to stop reading for more data. See How to know when HTTP-server is done sending data for more info.

In the working example you closed the client socket, which tells the browser there is no more data - for your ambitions this might be enough if you don't want the browser to respond.

于 2013-03-06T14:58:08.747 回答