Windows块上的Socket InputStream并交换查询 [英] Socket InputStream on windows blocks and swaps queries

查看:81
本文介绍了Windows块上的Socket InputStream并交换查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的ServerSocket程序,并通过URL从两个浏览器选项卡连接到它: http://127.0.0.1:8080/Q1 ,并在10秒钟的暂停后 http://127.0.0.1:8080 /Q2 .

I'm writing a simple ServerSocket program and connecting to it from two browser tabs by urls: http://127.0.0.1:8080/Q1 and after a 10 sec pause http://127.0.0.1:8080/Q2.

我的问题是,在具有java10的ubuntu18机器上,它可以快速而良好地工作.但是在两台Win机器上(我的win8.1和java8以及其他win10和java11),它在第一次查询后挂断,并且在长达2分钟的时间内什么都不做,如果我不发送第二次查询,服务器将保持安静.如果我发送第二个查询,我会在浏览器中得到两个查询的结果(先交换顺序是第一个Q2,然后是Q1),并且我的服务器报告读取了两个查询,接下来的两个浏览器也显示了收藏夹图标(也被交换-在90%的情况下,第一个Q2然后是Q1).

My problem is that on ubuntu18 machine with java10 it works quick and fine. But on two win machines (my win8.1 with java8 and other win10 with java11) it hangs up after first query and does nothing for up to 2 minutes, server stays quiet if I don't send second query. If I send second query I get result for both queries (in swaped order first Q2 then Q1) in browser and my server reports reading both queries and next two browser does for favicons (swaped too - first Q2 then Q1 in 90% cases).

如果我在启动Q1之后只是等待(没有启动Q2),则首先我看到Socket#1已创建并且正在等待.等待2分钟,然后关闭.然后,服务器会立即创建#2套接字并在Q1上快速回答,然后关闭,创建#3套接字获取并回答浏览器发送的Q1-favicon查询.一切都做得非常快速和美观.我不想在#1套接字上暂停一下.我在ubuntu18机器上没有它.

And if I just wait after starting Q1 (without starting Q2), first I see Socket#1 is created and it waits. It waits for 2 minutes and closes. Then in a moment server creates #2 sockets and quickly answers on Q1, closes, creates #3 socket gets and answers Q1-favicon query, that browser sent. All done very fast and beautiful. I would like not to have this pause with #1 Socket. I don't have it on ubuntu18 machine.

我做错了什么?还是我的赢奖机出问题了?您的获胜机器是否在不等待第二个查询的情况下处理第一个查询?

What am I doing wrong? Or is it something wrong with my win machines? Does your win machines process first query without waiting for second query?

import java.io.PrintWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.Socket;
import java.net.ServerSocket;
import java.util.stream.Collectors;
import java.io.IOException;

public class localhost {
    public static void main(String[] args) {
        int portNumber = 8080;
        System.out.println("Started...");
        String s="#";
        String ans="#";
        try (
        ServerSocket serverSocket = new ServerSocket(portNumber)){
            String httpResponse;
            int i=0;
            while(true){
                i++;
                System.out.println("Creating new Clien Socket #"+i);
                try( Socket clientSocket = serverSocket.accept()){
                    System.out.println("Accepted... "+clientSocket.toString());
                    try(InputStream in=clientSocket.getInputStream();){
                        {byte[] buffer = new byte[10000];
                        int total = in.read(buffer);
                        if (total>0) {
                            s = new String(java.util.Arrays.copyOfRange(buffer, 0, total));
                            System.out.println(s);

                            httpResponse = "HTTP/1.1 200 OK\r\n\r\nHello, there, "+ s.substring(5,7) +"!";
                            OutputStream out=clientSocket.getOutputStream();
                            out.write(httpResponse.getBytes());
                        } else { System.out.println("Empty..."); }
                        System.out.println("Finished #"+i+" "+clientSocket.toString());
                        System.out.println();}
                    }
                }
            }

        } catch(Exception e){
            System.out.println("Error: "+e);
        }
        System.out.println("I'm done "+s);
    }
}

UPD:对于没有Q2情况的Q1,如果我没弄错,Wireshark显示两个套接字-50410和50411,而服务器accept()-ed仅一个-50411.貌似50410导致等待,这个空查询和它来自哪里?

UPD: For Q1 without Q2 case if I'm getting it right Wireshark shows two sockets - 50410 and 50411, while server accept()-ed only one - 50411. Looks like 50410 causes waiting, what is this empty query and where is it coming from?

推荐答案

链接表示这可能是由预测网络操作以改善页面加载性能"设置引起的.因此,我尝试了一个一个地关闭预测设置,并成功了.在Chrome版本73.0.3683.86(官方内部版本)(64位)中,此行为是由Chrome设置使用预测服务更快地加载页面"引起的.

In a similar question I found a useful link saying it may be caused by "Predict network actions to improve page load performance" setting. So I tried turning off prediction settings one by one and it worked. In chrome version 73.0.3683.86 (Official Build) (64-bit) this behavior was caused by chrome setting "Use a prediction service to load pages more quickly" turned on.

因此,只需进入设置->高级->隐私和安全->使用预测服务可以更快地加载页面并将其关闭. (或者,如果您的Chrome版本没有该功能,请尝试关闭类似的设置).

So in just go to setting -> advanced -> privacy and security -> Use a prediction service to load pages more quickly and turn it OFF. (Or, if you don't have it in your chrome version, try turning off similar settings ).

这篇关于Windows块上的Socket InputStream并交换查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆