android--string Processing

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.