How to use CFNetwork to get byte array from sockets?

Posted by Vic on Stack Overflow See other posts from Stack Overflow or by Vic
Published on 2010-06-17T18:44:32Z Indexed on 2010/06/17 19:13 UTC
Read the original article Hit count: 397

Hi,

I'm working in a project for the iPad, it is a small program and I need it to communicate with another software that runs on windows and act like a server; so the application that I'm creating for the iPad will be the client.

I'm using CFNetwork to do sockets communication, this is the way I'm establishing the connection:

char ip[] = "192.168.0.244";
NSString *ipAddress = [[NSString alloc] initWithCString:ip];

/* Build our socket context; this ties an instance of self to the socket */
CFSocketContext CTX = { 0, self, NULL, NULL, NULL };

/* Create the server socket as a TCP IPv4 socket and set a callback */
/* for calls to the socket's lower-level connect() function */  
TCPClient = CFSocketCreate(NULL, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketDataCallBack, (CFSocketCallBack)ConnectCallBack, &CTX);

if (TCPClient == NULL)
  return;

/* Set the port and address we want to listen on */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
addr.sin_addr.s_addr = inet_addr([ipAddress UTF8String]);

CFDataRef connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));
CFSocketConnectToAddress(TCPClient, connectAddr, -1);
CFRunLoopSourceRef sourceRef = CFSocketCreateRunLoopSource(kCFAllocatorDefault, TCPClient, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), sourceRef, kCFRunLoopCommonModes);
CFRelease(sourceRef);
CFRunLoopRun();

And this is the way I sent the data, which basically is a byte array

/* The native socket, used for various operations */
// TCPClient is a CFSocketRef member variable
CFSocketNativeHandle sock = CFSocketGetNative(TCPClient);

Byte byteData[3];
byteData[0] = 0;
byteData[1] = 4;
byteData[2] = 0;

send(sock, byteData, strlen(byteData)+1, 0);

Finally, as you may have noticed, when I create the server socket, I registered a callback for the kCFSocketDataCallBack type, this is the code.

void ConnectCallBack(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
    // SocketsViewController is the class that contains all the methods
    SocketsViewController *obj = (SocketsViewController*)info;

    UInt8 *unsignedData = (UInt8 *) CFDataGetBytePtr(data);

    char *value = (char*)unsignedData;
    NSString *text = [[NSString alloc]initWithCString:value length:strlen(value)];
    [obj writeToTextView:text];
    [text release]; 
}

Actually, this callback is being invoked in my code, the problem is that I don't know how can I get the data that the windows client sent me, I'm expecting to receive an array of bytes, but I don't know how can I get those bytes from the data param.

If anyone can help me to find a way to do this, or maybe me point me to another way to get the data from the server in my client application I would really appreciate it.

Thanks.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone-sdk