[英]Is it possible to put a delay on BufferedReader reading from socket
I am wondering is it possible to put a delay on how often a packet is read by the buffered reader when being read from a socket. 我想知道是否有可能延迟从套接字读取缓冲读取器读取数据包的频率。 At the moment there is a server which streams data over local host to a client which reads in the data and splits it.
目前,有一台服务器通过本地主机将数据流传输到客户端,该客户端读取数据并将其拆分。 I would like the data not to be read continuously and rather would like to read in a packet every 1/2 second.
我希望数据不能被连续读取,而是希望每1/2秒读取一个数据包。 Below is the code I currently have:
以下是我目前拥有的代码:
Set up of the BufferedReader: 设置BufferedReader:
` `
Socket clientSocket = new Socket("localhost", 1240);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
' '
This section of code is where the packets are read in: 这部分代码是读取数据包的位置:
while (true)
{
String line = inFromServer.readLine();
if (line != null){
String[] packetData = line.split(",");
if (packetData.length == 5)
{
x = Double.parseDouble(packetData[1]);
y = Double.parseDouble(packetData[2]);
z = Double.parseDouble(packetData[3]);
}
}
}
If anyone could give me help on this it would be greatly appreciated. 如果有人可以在这方面给我帮助,将不胜感激。 Thanks.
谢谢。 Daniel
丹尼尔
Perhaps put a 500 millisecond delay into the loop after reading & processing each line, then? 也许在读取和处理每条线之后将500毫秒的延迟放入循环中,然后呢? See
Thread.sleep()
. 参见
Thread.sleep()
。
See: 看到:
Do you really care about packets (low-level networking stuff) or are you just interested in throttling to 1 line per 0.5 seconds. 您是否真的关心数据包(低级网络功能),还是只希望每0.5秒将数据节流到1条线路。 In the latter case just put Thread.sleep(500) in the while loop.
在后一种情况下,只需将Thread.sleep(500)放入while循环中即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.