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.
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:
In your communication protocol, include a length field or some command code that indicates the length.
Use asynchronous read and write or use a thread dedicated to deal with communication.
Before jumping on your keyboard and code, think about your app architecture, its components, their function and how they relate to each other.