javanetwork-programmingservercyclemultiplayer

Java - How to get number of bytes in a DataInputStreaam?


I am currently writing the server side of a multiplayer game I am working on. I need to know how much data is being received on the server socket, so I can cycle through each byte. For example, If I call writeUTF() twice, how will I cycle through each without getting an exception?

My first attempt was to simply try catch an EOFException. But you can't seem to catch those. So, how do I get the amount of data being received in a DataInputStream? If you need a code sample, please let me know and I will edit the question accordingly.


Solution

  • Your calls to read data from a socket will block until data is received. There is no way to find out how much data is in a stream. You have several ways to solve that and these are some suggestions: