使用CocoaAsyncSocket读取套接字数据的问题

时间:2016-09-12 02:10:37

标签: objective-c macos sockets binary-data cocoaasyncsocket

我从CocoaAsyncSocke gibhub获得了从服务器读取套接字数据的框架,但是我无法读取正确的jpg内容,有人可以告诉我代码中有什么问题吗?

套接字的帧二进制格式。 [二进制格式] [2]

 - (void)socket:(GCDAsyncSocket * __nonnull)sock didConnectToHost:(NSString * __nonnull)host port:(uint16_t)port
{
    NSLog(@"connect to host:%@ %d",host,port);

    [sock readDataWithTimeout:-1 tag:0];
}
- (void)socket:(GCDAsyncSocket * __nonnull)sock didReadData:(NSData * __nonnull)data withTag:(long)tag
{
    NSInteger len = data.length;

//    Byte *byteArray = (Byte *)[data bytes];

//    for (int i=0 ; i<[data length]; i++) {
//        NSLog(@"-----row =%d  and byte = %d",i,byteArray[i]);
//    }

    if (tag == 0) {


        NSLog(@"tag 0:data:%@ len:%ld",data,data.length);
        [data writeToFile:@"/tmp/t.png" atomically:YES];

        [sock readDataToLength:4 withTimeout:-1 tag:1];
    }
    else{
        NSLog(@"data:%@ len:%ld %@",data,data.length,data);

        UInt32 readLength;
        [data getBytes:&readLength length:4];
        [sock readDataToLength:readLength withTimeout:-1 tag:0];
    }
}

0 个答案:

没有答案