views:

91

answers:

2

Essentially I'm sending data to a Java Socket Server from an iPhone app however something rather strange happens, it doesn't receive the data until the iPhone application is closed! I'm sure there is something I'm missing but I just can't seem to find it, it's all quite odd.

Here is how my connection is created:

-(CFSocketRef)initSocket {
    CFSocketContext context = {
        .version = 0,
        .info = self,
        .retain = NULL,
        .release = NULL,
        .copyDescription = NULL
    };

    sockety = CFSocketCreate(
        kCFAllocatorDefault,
        PF_INET,
        SOCK_STREAM,
        IPPROTO_TCP,
        kCFSocketDataCallBack^kCFSocketConnectCallBack,
        socketCallBack,
        &context
        );
    uint16_t port = 4444;

    struct sockaddr_in addr4;

    memset(&addr4, 0, sizeof(addr4));
    addr4.sin_family = AF_INET;
    addr4.sin_len = sizeof(addr4);
    addr4.sin_port = htons(port);

    const char *ipaddress = "192.168.1.5";

    inet_aton(ipaddress, &addr4.sin_addr);

    NSData *address = [NSData dataWithBytes:&addr4 length:sizeof(addr4)];

    CFSocketError error = CFSocketConnectToAddress(sockety, (CFDataRef)address, 1);

    if(error != kCFSocketSuccess ) 
    { 
        Faliure = YES;
    } 
    else{
        ViewNo = 2;
    }
    CFRunLoopSourceRef source;

    source = CFSocketCreateRunLoopSource(NULL, sockety, 1);
    CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
    CFRelease(source);
    return sockety;
}

Heres how the message is sent:

const char *sendStrUTF = [sentmessage UTF8String];
NSData *dataOut = [NSData dataWithBytes: sendStrUTF length: strlen(sendStrUTF)];
CFSocketSendData(sockety, NULL, (CFDataRef) dataOut, 0);

Any help would be greatly appreciated!

Thanks in advance,
Ozzie

A: 

Is your call to CFSocketSendData being done on a (blocked) GUI thread?

I would experiment with wrapping those 3 lines in a performSelectorInBackground / or after delay combinations.

David Sowsy