java:客户端/服务器套接字中的I/O流导致300ms的随机延迟 [英] java: I/O stream in client/server socket causing randomly 300ms delay

查看:73
本文介绍了java:客户端/服务器套接字中的I/O流导致300ms的随机延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些需要服务器和客户端应用程序之间的低延迟通信的项目.两者都在我的计算机上运行,​​因此已连接到本地主机.

I'm working on some project that requires a low latency communication between a server and a client application. Both runs on my computer so it is connected to localhost.

通过 DataInputStream DataOutputStream 以及套接字连接的数据传输正常进行,并且99%的时间测量到的ping为0ms或1ms,但是有时通信会随机延迟300毫秒.

The data transfer via DataInputStream and DataOutputStream and the socket connection works properly, and 99% of the time the measured ping is 0ms or 1ms, however sometimes the communication gets randomly delayed by 300ms.

诸如网络游戏之类的常规应用程序运行良好,没有任何滞后现象",因此,我确定这不是我的计算机出现的问题.

Normal applications like network games run fine without any "lagspikes", so I'm sure it's not an issue with my computer.

我非常怀疑延迟发生的时间总是 300ms ,而没有发生延迟的时间是 0ms或1ms ,所以有些事情发生了,但是我可以不知道是什么.

I find it very suspicious that the delay is always exactly 300ms when it occurs and 0ms or 1ms when it does not occur, so there is something happening but I can not find out what.

我记录了时间戳,直到弄清楚它在服务器和客户端中的任何一个发生

I logged with timestamps until figuring out it happens both at server and client at any of the

in.read();

,其中 in DataInputStream ,因此我假设延迟不直接出现在我的代码中.

with in being the DataInputStream so im assuming the delay is not directly in my code.

有人可以向我解释,在Java中,I/O流或套接字导致300ms的延迟是什么,我该如何解决?我为服务器/客户端更改的唯一一件事是

Can someone explain to me, what in Java, the I/O stream or the sockets is causing a 300ms lag and how do I solve it? The only thing I changed for server/client is

socket.setPerformancePreferences(0, 1, 0);

但是它并没有改变任何事情.

but it didn't change a thing.

我的代码有点大,但是服务器和客户端应用程序相关部分的一般结构如下:

My code is a bit big but the general structure for the relevant part of both server and client application looks like:

while(still_alive) {
  out.writeInt(data1);
  out.writeInt(data2);
  out.writeDouble(data3);
  data1 = in.readInt();
  data2 = in.readInt();
  data3 = in.readDouble();
  DoSomethingWithData(data1, data2, data3);
}

提前谢谢!

Kaisky

推荐答案

socket.setPerformancePreferences(1, 0, 2); // here latency is 0.
socket.setTcpNoDelay(true); // for client as well as server.

尝试一下.它将带来巨大的变化.并且DataOutputStream和DataInputStream可以完美地工作.

try this out. it will make great change. and DataOutputStream and DataInputStream work perfect with it.

这篇关于java:客户端/服务器套接字中的I/O流导致300ms的随机延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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