* 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.