0

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. Below is the code I currently have:

Set up of the 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

dpg
  • 3
  • 3

2 Answers2

0

Perhaps put a 500 millisecond delay into the loop after reading & processing each line, then? See Thread.sleep().

See:

Community
  • 1
  • 1
Thomas W
  • 13,940
  • 4
  • 58
  • 76
0

Do you really care about packets (low-level networking stuff) or are you just interested in throttling to 1 line per 0.5 seconds. In the latter case just put Thread.sleep(500) in the while loop.

dlipofsky
  • 289
  • 1
  • 4
  • 18