简体   繁体   English

Java代理-无法收到主机/套接字问题的响应

[英]Java Proxy - Can't receive response from host/Socket issue

I am coding a simple Java proxy. 我正在编写一个简单的Java代理。 The general architecture was given to me (method signatures etc), and this is how the Main class looks: 给出了一般的体系结构(方法签名等),这是Main类的外观:

private static Socket clientSocket;
private static ServerSocket client;
private static int myPort;

public static void init(int port) throws IOException {
    client = new ServerSocket(port);
    clientSocket = client.accept();
}
public static void handle(Socket clientSocket) {
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        HttpRequest httpRequest = new HttpRequest(in);

        String hostname = httpRequest.getHost();
        //443 hardcoded from reading the http headers. 
        //Testing using isitchristmas.com
        Socket serverSocket = new Socket(hostname, 443);
        BufferedReader out = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
        HttpResponse httpResponse = new HttpResponse(out);
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void main(String args[]) {
    try {
        myPort = Integer.parseInt(args[0]);
        try {
            System.out.println("Initializing socket...");
            init(myPort);
        } catch (IOException e) {
            System.out.println("[ERROR]: " + e.getMessage());
        }
        handle(clientSocket);
    } catch (Exception e) {
        System.out.println("[ERROR]: " + e.getMessage());
    }
}

However the console hangs and never completes a request when reading in HttpResponse class: 但是,在读取HttpResponse类时,控制台将挂起并且永远不会完成请求:

    public HttpResponse(BufferedReader fromServer) throws IOException {
    String line;
    String statusLine = "";
    // Never goes past here
    while ((line = fromServer.readLine()) != null) {
        if (line.isEmpty()) {
            break;
        }

        if (line.toLowerCase().contains("status")) {
            statusLine = line;
        }

        response.append(line);
    }

    if (!response.toString().isEmpty()) {
        getDataAndHeadersFromResponse(response.toString());
        System.out.println("\n\nHTTP Response:\n");
        System.out.println("Status Line: " + statusLine);
        System.out.println("Header Lines: " + headerLines + "\n\n");
        System.out.println("Data: " + data);
    }

}

I suspect it has something to do with how I am creating the sockets... not calling close() on ServerSocket gives off a Address already in use: JVM_Bind exception. 我怀疑这与我创建套接字的方式有关...在ServerSocket上不调用close()会释放出一个已在使用的地址:JVM_Bind异常。 I also don't seem to get the serverSocket parameters right. 我似乎也没有正确获得serverSocket参数。 As you can tell by now I'm not very versed in socket programming. 正如您现在所知道的,我对套接字编程不是很精通。 What is wrong here? 怎么了

  • You need to listen on port 80: nothing in the assignment about HTTPS. 您需要侦听端口80:关于HTTPS的分配中没有任何内容。
  • The request body won't be terminated by end of stream, because the client still has the socket open to read the response. 请求主体不会在流的末尾终止,因为客户端仍然打开套接字以读取响应。
  • You need to read the Content-length header from the client and then read exactly that many bytes of request body. 您需要从客户端读取Content-length标头,然后精确读取请求主体的这么多字节。
  • You therefore cannot solve this problem with a BufferedReader , as you have to count bytes, not chars. 因此,您不能使用BufferedReader解决此问题,因为您必须计算字节而不是字符。
  • Having sent the request on to the server, all you really need to do afterwards is copy the response bytes directly from the server to the client: not what it says in the assignment. 将请求发送到服务器后,您真正需要做的就是将响应字节直接从服务器复制到客户端: 而不是分配中的内容。
  • To stick with what the assignment says, you would similarly have to read the Content-Length header from the server, and all the other headers, and the blank line, and exactly content-length bytes of response from the server, and copy them all to the client. 为了遵守作业中所说的内容,类似地,您必须从服务器读取Content-Length标头,并从服务器读取所有其他标头,空行以及确切的内容长度字节,然后将它们全部复制给客户。 You can see this is a waste of time compared to just copying bytes. 与仅复制字节相比,您可以看到这是浪费时间。
  • The line terminator in HTTP is \\r\\n , not \\n . HTTP中的行终止符是\\r\\n ,而不是\\n

The assignment is deficient in: 作业不足:

  • specifying the infeasible BufferedReader 指定不可行的BufferedReader
  • then inconsistently specifying DataInputStream for reading from the server 然后不一致地指定要从服务器读取的DataInputStream
  • there is no reason to use DataInputStream at all: InputStream would suffice 完全没有理由使用DataInputStreamInputStream就足够了
  • not referring to RFC 2616 or its predecessors or successors 不涉及RFC 2616或其前身或后继
  • not specifying whether HTTP 1.0 or 1.1 没有指定HTTP 1.0或1.1
  • not saying more about 'to cache Web pages', which is only mentioned on page 2 and never thereafter. 不会多说“缓存网页”,只有在第2页提到过,此后再也没有。

Students should complain. 学生应该抱怨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM