0

I'm trying to use GCDAsyncSocket in my iOS app. I've been following all the step provided in the CocoaAsyncSocket's wiki. Here is what I'm doing:

 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];

I already included too frameworks dependences: Security & CFNetwork, and included in my class the respective delegate. Do I need any other configuration to use it?

When I run this example I get this error:

[NSMallocBlock bytes]: unrecognized selector sent to instance 0x6b7abe0 'NSInvalidArgumentException', reason: '-[NSMallocBlock bytes]: unrecognized selector sent to instance 0x6b7abe0'

And it occurs on this line of GCDAsyncSocket.m

const uint8_t *buffer = (const uint8_t *)[currentWrite->buffer bytes] + currentWrite->bytesDone;
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
adheus
  • 3,985
  • 2
  • 20
  • 33

1 Answers1

0

try use this to convert your data, from string to data

    +(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;
}
user774150
  • 962
  • 2
  • 10
  • 24