【问题标题】:Error trying to write data using GCDAsyncSocket on iOS 5.1尝试在 iOS 5.1 上使用 GCDAsyncSocket 写入数据时出错
【发布时间】:2012-08-07 18:38:15
【问题描述】:

我正在尝试在我的 iOS 应用中使用 GCDAsyncSocket。我一直在遵循 CocoaAsyncSocket 的 wiki 中提供的所有步骤。这是我正在做的事情:

 GCDAsyncSocket socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];

    NSError *err = nil;
    if (![socket connectToHost:@"192.168.0.129" onPort:2811 error:&err]) // Asynchronous!
    {
        // If there was an error, it's likely something like "already connected" or "no delegate set"
        NSLog(@"I goofed: %@", err);
    }

    uint8_t buffer[2] = "1\n";

    NSData *data = [NSData dataWithBytes: &buffer length: sizeof(buffer)];
    [socket writeData:data withTimeout:10 tag:1];

我已经包含了框架依赖项:Security 和 CFNetwork,并在我的课程中包含了相应的委托。我需要任何其他配置才能使用它吗?

当我运行这个例子时,我得到了这个错误:

[NSMallocBlock bytes]:无法识别的选择器发送到实例 0x6b7abe0 'NSInvalidArgumentException',原因:'-[NSMallocBlock bytes]:无法识别的选择器发送到实例 0x6b7abe0'

而且发生在GCDAsyncSocket.m这一行

const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone;

【问题讨论】:

    标签: ios objective-c stream ios5.1 gcdasyncsocket


    【解决方案1】:

    尝试使用它来转换你的数据,从字符串到数据

        +(NSData *) DataToHex: (NSString *) string
    {
        string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
        int stringLength = string.length;
        NSMutableData *hexStringArray = [[NSMutableData alloc] init];
        [hexStringArray setLength:0];
        unsigned char whole_byte;
    
        char byte_chars[3] = {'\0','\0','\0'};
        int i;
        for (i=0; i < stringLength/2; i++) 
        {
            byte_chars[0] = [string characterAtIndex:i*2];
            byte_chars[1] = [string characterAtIndex:i*2+1];
            whole_byte = strtol(byte_chars, NULL, 16);
            [hexStringArray appendBytes:&whole_byte length:1]; 
        }                                                                       //this is auto way
    
        return hexStringArray;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多