Android: ObjectInputSteam.readFully() gets Stuck

我的梦境 提交于 2019-12-12 02:37:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!