简体   繁体   中英

Write to socket directly and get TCP ZeroWindow

Just as you can see, I send a request to the specific socket every 10 seconds directly( to remain alive and be detected ), but it can only send twice. Using wireshark, I found that the third package I send has TCP ZeroWindow and it cannot send to the server. So does the following packages. Usually, the Window Size should remain at a normal level instead of decreasing all the time. What's wrong with the source code? Any help appreciated!

Three packages I got:

第一包第二包第三包

Source code: very simple

public class pediy {

    public static void main(String[] args) throws IOException {

        URL url = new URL("http://bbs.pediy.com");
        Socket socket = null;
        PrintWriter os = null;
        BufferedReader is = null;

        while(true) {

            socket = new Socket(url.getHost(), 80);
            os = new PrintWriter(socket.getOutputStream());

            String request = "GET / HTTP/1.1\nHost: bbs.pediy.com\nProxy-Connection: keep-alive\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nUser-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17 CoolNovo/2.0.6.12\nAccept-Encoding: gzip,deflate,sdch\nAccept-Language: en-US;q=0.6,en;q=0.4\nAccept-Charset: utf-8;q=0.7,*;q=0.3\n";
            try {
                while (true) {

                    os.println(request);
                    os.flush();

                    System.out.println("Finished");
                    Thread.sleep(1000*10);
                }

            } catch (Exception e) {
                System.out.println("Error" + e);
            } finally {
                CloseAll(socket, os);
            }
        }
    }

    private static void CloseAll(Socket socket, PrintWriter os) throws IOException {
        if(socket != null) socket.close();
        if(os != null) os.close();
    }
}

这仅表示TCP连接的另一端没有使用(读取)您发送的数据,因此TCP流控制启动以降低发送者的速度。

Well,

  1. You aren't sending valid HTTP. The line terminator in HTTP is \\r\\n , not just \\n , and there needs to be a blank line after the headers.

  2. You aren't reading the responses.

So the other end will be having some difficulty. You're lucky it hasn't closed the connection already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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