IOS byte intercept socket parsing

Source: Internet
Author: User

* Detail Learning-intercepting socket data parsing for IOS byte arrays *

I recently made a socket long connection. Meet some problems, I hope to give you some information about the brick friends

First question to meet:
The returned socket data has no problem with nsasciistringencoding letter parsing. But the Chinese alphabet is garbled. This problem is due to the coding format of your socket information, for example, my socket information is a custom pattern that uses the first 9 bytes as a fixed header. These 9 bytes are encoded in ASCII, the length of the content behind the use of UTF8 to do, you directly use ASCII parsing is garbled, with UTF8 is not resolved. The corresponding content must be intercepted. The corresponding decoding format is OK.

Second question:
Because the socket is a long connection. So, if some people use OC with the Rangeofstring method to judge JSON to intercept, there will be problems, the contents of the socket is connected, so the content of the socket is not interrupted this interception will cause socket adhesion, solution. Can take a custom message header way to put the length of the data in the first 9 of the information back to their own

The third problem is parsing:

Parsing must be resolved from byte start, and the previous 9 bytes get the length of the command behind it, the JSON length.
For example, to go to a byte array after getting data

byte Resverbyte = (byte) [data bytes];

When you get the command length and the JSON length, you can split the byte array to get the relevant information

(void) Bytesplit2byte: (byte[]) src orc: (byte[]) ORC begin: (Nsinteger) Begin count: (Nsinteger) count{

for (Nsinteger i = begin; I < Begin+count; i++) {
Orc[i-begin] = Src[i];
}
}

Get the desired byte[], you can turn it into data and turn it into nsstring.

Related Article

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.