问题
Situation : I send a boolean variable via ObjectOutputStream
from a client to a server. Thereafter, I immediately send a string data via writeChars()
function. The server receives the boolean via readBoolean()
and spawns a thread accordingly. It reads the string using readFully(bytes[])
.
Problem : Server hangs at the line ObjectInputStream.readFully(bytes[])
.
Should I just increase the byte size, or is the whole approach wrong ?
回答1:
If this is a String
and you are using ObjectOutputStream
you should just call writeUTF()
to write it and readUTF()
to read it. This method actually first writes the number of bytes to the stream and then the bytes. On the receiving end, it reads the number of bytes and then knows how many bytes to expect.
来源:https://stackoverflow.com/questions/21380204/android-objectinputsteam-readfully-gets-stuck