简体   繁体   English

如何在 Simple Java Socket Server 中发送关闭块?

[英]How to send a closing chunk in Simple Java Socket Server?

I have a server A that uses Httpcore, HttpcoreNIO for HTTP communications.我有一台服务器 A,它使用 Httpcore、HttpcoreNIO 进行 HTTP 通信。 I have written a Simple Java Socket Server B to use as a backend.我写了一个简单的 Java Socket Server B 用作后端。

public void run() {
        String expectedResponse = "HTTP/1.0 200 OK\r\nServer: testServer\r\n" +
                                  "Content-Type: text/html\r\n" +
                                  "Transfer-Encoding: chunked\r\n" +
                                  "Transfer-Encoding: chunked\r\n" +
                                  "\r\n" + "<HTML>\n" + "<!DOCTYPE HTML PUBLIC " +
                                  "\"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" +
                                  "<HEAD>\n" + " <TITLE>Hello World</TITLE>\n" +
                                  "</HEAD>\n" + "\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                                  "<H1 ALIGN=\"CENTER\"> Hello</H1>\n" +
                                  "Sample text 123\n" +
                                  "test 123:\n" + "<PRE>";
        try {
            serverSocket = new ServerSocket(port);
            System.err.println("Server starting on port : " + port);
            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.err.println("Client connected");
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
                while (true) {
                    String s;
                    if ((s = in.readLine()) != null) {
                        System.out.println(s);
                        if (!s.isEmpty()) {
                            continue;
                        }
                    }
                    out.write(expectedOutput);
                    System.err.println("connection terminated");
                    out.close();
                    in.close();
                    clientSocket.close();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

When server A receives a response from the backend B, I am getting the following exception.当服务器 A 收到来自后端 B 的响应时,我收到以下异常。

Premature end of chunk coded message body: closing chunk expected

It seems that Httpcore is expecting a Closing chunk (or Zero-length chunk) before closing the connection.似乎 Httpcore 在关闭连接之前需要一个 Closing 块(或零长度块)。

Can someone let me know how to send this closing chunk from the above Java Simple Socket Server?有人可以让我知道如何从上面的 Java 简单套接字服务器发送这个关闭块吗?

Transfer encoding chunked http header means that you are sending the http body in chunks.传输编码分块 http header 表示您正在分块发送 http 正文。 Each chunk starts with a "header" with the chunk size in bytes, and "/r/n", and then the chunk itself.每个块都以“头”开头,块大小以字节为单位,“/r/n”,然后是块本身。 Once you will do it, and you are done, you should send "0" and "/r/n".一旦你完成了,你应该发送“0”和“/r/n”。

BTW, is there a reason you are sending the "transfer encoding chunked" http header twice?顺便说一句,您是否有理由两次发送“传输编码分块”http header?

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

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