[英]Java: Writing to socket with DataOutputStream slows down, then stops
while (true) {
loc = getLocationOnScreen();
out.writeLong((long)loc.getX());
out.writeLong((long)loc.getY());
out.flush();
i++;
System.out.println(i);
}
This code will merrily go along for about 3 seconds, before it slows, then stops. 这段代码会缓慢运行约3秒钟,然后变慢,然后停止。
i
ends at about 16491
, never to continue increasing. i
在大约16491
处结束,永不继续增加。 What's going on here? 这里发生了什么?
It is slowing down because , at the receiving end you might be doing long task using the value sent by you , before reading the next value. 它之所以变慢是因为,在接收端,在读取下一个值之前,您可能会使用您发送的值来执行长任务。 So the rate at which the other side is reading the data is smaller than the rate at which you are sending data to that.
因此,另一端读取数据的速率小于您向其发送数据的速率。 This keeps filling the receiving buffer at the other side and sending buffer at sender side.
这将继续在另一侧填充接收缓冲区,并在发送方填充发送缓冲区。 Eventually when both buffers are filled completely the transmission of data is halted.
最终,当两个缓冲区都完全装满时,数据传输将停止。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.