You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is, there's no terminating char in data stream and the only way to distinguish the length of comming "response packets" is to read and parse their first 5 bytes and use that for cutting data into "packets" and making callbacks to my delegate. Currently, I'm using AsyncSocket as shown in code fragment above, and some callbacks "onSocket:didReadData:withTag:" are already good length data packets (received NSData), but some are merged. IMHO I need explicitly to tell AsyncSocket how to interpret and cut coming data into packets when reading.
The first byte of data tells me the length of the rest of the packet. Header packets are in my case recognisable by bit 0 = 1, bit 4 = 0, bit 7 = 0. Is there an easy way to do this with (GDC)AsyncSocket?
Is the proper way to do a Read-with-length: 1 until I encounter a header packet and then Read-with-length: bodySize?
Zyphrax, I found the solution (at least for situations when for every request you receive a response). Yes, you need to call readToLength in onSocket:didWriteDataWithTag: in AsyncSocket delegate method:
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag {
...
[sock readDataToLength:<preferred size of response packet> withTimeout:myTimeout tag:tag];
//instead of
//[sock readDataWithTimeout:myTimeout tag:tag];
}
If you don't use terminator then for every request you make (for every write to server) you need to tell AsyncSocket when to callback on read.
I rewrote the question to be more precise on the solution I'm looking for:
Is it possible to do the following with AsyncSocket:
while ("response packet" is not read) do {
}
Here's my current usage of AsyncSocket:
I'm writing a data via socket with AsyncSocket:
and receiving a callback in my delegate class:
The problem is, there's no terminating char in data stream and the only way to distinguish the length of comming "response packets" is to read and parse their first 5 bytes and use that for cutting data into "packets" and making callbacks to my delegate. Currently, I'm using AsyncSocket as shown in code fragment above, and some callbacks "onSocket:didReadData:withTag:" are already good length data packets (received NSData), but some are merged. IMHO I need explicitly to tell AsyncSocket how to interpret and cut coming data into packets when reading.
Original post you can find here: http://stackoverflow.com/questions/9359391/asyncsocket-getting-merged-two-packets-instead-of-separate-two-packets
The text was updated successfully, but these errors were encountered: