小木屑 2014-10-31 07:23 采纳率: 0%
浏览 1123

关于Socket中的输出流缓存问题

为什么以下程序中Send Request cost的输出在第二次之后都是0或者1ms?
public class MyThread implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
getTime();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void getTime() {
String strServer = "www.baidu.com";
Socket socket = null;
try {
int port = 80;
InetAddress addr = InetAddress.getByName(strServer);
long time3 = System.currentTimeMillis();
socket = new Socket(addr, port);
long time4 = System.currentTimeMillis();
System.out.println("Connect cost:" + (time4 - time3) + "ms");
long time5 = System.currentTimeMillis();
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
wr.write("GET / HTTP/1.0\r\n");
wr.write("HOST: www.baidu.com\r\n");
wr.write("Accept:*/*\r\n");
wr.write("\r\n");
wr.flush();
long time6 = System.currentTimeMillis();
System.out.println("Send Request cost:" + (time6 - time5)+ "ms");
wr.close();

    } catch (Exception e) {
        System.out.println("Website not access");
        e.printStackTrace();
    }
}

public static void main(String[] args) throws InterruptedException {
    MyThread s = new MyThread();
    new Thread(s).start();
}

}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:35
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?