(1) The two parties agree that each transmission message is a few characters before the message length information, as follows
The original message is: ASDF
The socket message sent is: 0004ASDF
0004 indicates the length of the message, and the receiver receives the four-bit length before receiving the remaining messages.
(2) The two parties agree that a special mark is terminator, such as the agreement ==end== the end
The code is modified to the while (!) ==end== ". Equals (line = In.readline ())) continues to wait for receiving
This special terminator needs to be sent by both parties to send the end of the message.
The first solution is the most common. Some of the projects that you touch are this way.
Attach the use of the first method:
1. Get the length
byte New byte [5]; // Both parties agree that the first five digits indicate the length of the message 0, head.length); // Read message length information first String headlength New String (Head, GetEncoding ()); int headlength= integer.parseint (headlength);
byte New byte [Msglengthint]; // new byte array with a message length size 0, msgbody.length); // Read message information byte New byte [Msghead.length + msgbody.length]; // merging message lengths and message information if necessary 0, recmsg, 00, Recmsg, Msghead.length, msgbody.length);
1
android--string Processing