I have to implement a client for communication with the server by custom protocol based on XML
format. This is an application layer protocol, based on TCP
. So, my client sends a request XML
message and receives a response, also XML message. Now, I consider how to be ensured that I received whole mesage before I start parsing it.
I see two aproaches:
Receiving bytes to the some magic number that means end of message. It is the best approch (for my eye), yes?
But, it is possible that there is no magic number and size of message is not known. What about that case? I saw some clients for other protocols and I see something like that.
while(true){ r = socket.read(buffer, offset, 1024); if(r < 1024) break; offset += r; } // parse buffer
and I am not sure whether it is ok. It assumes that if we read less than 1024 bytes then the messae is completed. Is it ok?
What is a recommend way to solve it?
Socket
'sInputStream
. Take a look at what the differentread
methods do. – f1sh