请教,郁闷死了AsyncSocket接收大的数据时就接收的不完整
2个回答
展开全部
如果我没猜错的话,问题出在你的C#服务端!
看一下你代码中的这条语句
[self.outSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
参数[AsyncSocket CRLFData]表示读取至"\r\n"
查查你的C#服务端在返回数据时结尾有没有追加"\r\n",没有的话加上,否则didReadData永远不会执行。
另,使用开源三方库前,有文档的话好好看一下,没有的话就看一下头文件,大部分规范的开源代码在头文件中都会加入说明,这个问题头文件里说的很清楚
/**
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
*
* If the timeout value is negative, the read operation will not use a timeout.
*
* If you pass nil or zero-length data as the "data" parameter,
* the method will do nothing, and the delegate will not be called.
*
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
* Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
* a character, the read will prematurely end.
**/
- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
-
看一下你代码中的这条语句
[self.outSocket readDataToData:[AsyncSocket CRLFData] withTimeout:-1 tag:0];
参数[AsyncSocket CRLFData]表示读取至"\r\n"
查查你的C#服务端在返回数据时结尾有没有追加"\r\n",没有的话加上,否则didReadData永远不会执行。
另,使用开源三方库前,有文档的话好好看一下,没有的话就看一下头文件,大部分规范的开源代码在头文件中都会加入说明,这个问题头文件里说的很清楚
/**
* Reads bytes until (and including) the passed "data" parameter, which acts as a separator.
*
* If the timeout value is negative, the read operation will not use a timeout.
*
* If you pass nil or zero-length data as the "data" parameter,
* the method will do nothing, and the delegate will not be called.
*
* To read a line from the socket, use the line separator (e.g. CRLF for HTTP, see below) as the "data" parameter.
* Note that this method is not character-set aware, so if a separator can occur naturally as part of the encoding for
* a character, the read will prematurely end.
**/
- (void)readDataToData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
-
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用readDataToLength 通过数据报文长度来确认你要取的数据
报头里面记录报文格式信息 包含数据报文大小
再读报体
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
报体大小如果刚好同报头记录的数据大小一样
继续读取下一个报头
否则
修正偏移
if (self.dataLen == self.dataBody.length) {
[self readData:HaderSize withTag:tagReadHeader];//read for next package.
}
else {
NSInteger fixLen = dataLen - data.length;
if (fixLen>0) {
dataLen = fixLen;
[self readData:fixLen withTag:tagReadBody];
}
else {
[self disconnection];//for renconnection.
}
}
报头里面记录报文格式信息 包含数据报文大小
再读报体
- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
报体大小如果刚好同报头记录的数据大小一样
继续读取下一个报头
否则
修正偏移
if (self.dataLen == self.dataBody.length) {
[self readData:HaderSize withTag:tagReadHeader];//read for next package.
}
else {
NSInteger fixLen = dataLen - data.length;
if (fixLen>0) {
dataLen = fixLen;
[self readData:fixLen withTag:tagReadBody];
}
else {
[self disconnection];//for renconnection.
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询