简体   繁体   English

java:客户端/服务器套接字中的 I/O 流导致随机 300 毫秒延迟

[英]java: I/O stream in client/server socket causing randomly 300ms delay

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.两者都在我的计算机上运行,​​因此它连接到本地主机。

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.通过DataInputStreamDataOutputStream的数据传输和socket连接工作正常,99%的时间测得ping为0ms或1ms,但有时通信会随机延迟300ms。

Normal applications like network games run fine without any "lagspikes", so I'm sure it's not an issue with my computer.像网络游戏这样的普通应用程序运行良好,没有任何“滞后”,所以我确定这不是我的电脑的问题。

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.我觉得很可疑,延迟发生时总是恰好 300 毫秒,不发生时延迟总是 0毫秒或 1 毫秒,所以发生了一些事情,但我不知道是什么。

I logged with timestamps until figuring out it happens both at server and client at any of the我用时间戳记录,直到发现它发生在服务器和客户端的任何一个

in.read();

with in being the DataInputStream so im assuming the delay is not directly in my code.作为DataInputStream类这样的IM假设延迟是不能直接在我的代码。

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?有人可以向我解释一下,在 Java、I/O 流或套接字中是什么导致了 300 毫秒的延迟,我该如何解决? 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);
}

Thanks in advance!提前致谢!

Kaisky凯斯基

I've found the solution!我找到了解决方案!

It seems like DataInputStream and DataOutputStream are not suitable for low latency network but you should use BufferedInputStream and BufferedOutputStream instead.似乎DataInputStreamDataOutputStream不适合低延迟网络,但您应该使用BufferedInputStreamBufferedOutputStream代替。

It is a bit harder to use since every input is in bytes so you need to translate any int, double and String into byte arrays and translate them back, so i took a while to do that but now it works perfectly without any lag.使用起来有点困难,因为每个输入都是以字节为单位的,因此您需要将任何 int、double 和 String 转换为字节数组并将它们转换回来,所以我花了一些时间来做到这一点,但现在它可以完美运行,没有任何延迟。

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

try this out.试试这个。 it will make great change.它将带来巨大的改变。 and DataOutputStream and DataInputStream work perfect with it.和 DataOutputStream 和 DataInputStream 与它完美配合。

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

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