Re: CFSocket
Re: CFSocket
- Subject: Re: CFSocket
- From: Douglas Davidson <email@hidden>
- Date: Fri, 10 May 2002 10:41:38 -0700
On Thursday, May 9, 2002, at 01:53 PM, Matt Jarjoura wrote:
I really enjoyed the presentation given on Tue and Wed over CFSocket and
CFStreams.
My initial question comes to:
Has anyone created a concrete example for using CFSocket? the Examples
on
Jaguar use CFNetServices.
Here is the example I presented in the talk on Wednesday. It is rather
abbreviated--mainly so that it would fit on the slides--but it should
illustrate the main points. There are many other options and functions,
most of which are discussed in the CFSocket header.
Douglas Davidson
compile with cc -framework CoreFoundation:
#import <CoreFoundation/CoreFoundation.h>
#include <sys/socket.h>
#include <netinet/in.h>
void receiveData(CFSocketRef child, CFSocketCallBackType type, CFDataRef
address, const void *data, void *info) {
static char helloWorld[] = "HTTP/1.0 200 OK\r\n\r\nhello, world\r\n";
CFDataRef response = CFDataCreate(NULL, helloWorld,
strlen(helloWorld));
CFSocketSendData(child, NULL, response, 0.0);
CFRelease(response);
CFSocketInvalidate(child);
CFRelease(child);
}
void acceptConnection(CFSocketRef socket, CFSocketCallBackType type,
CFDataRef address, const void *data, void *info) {
CFSocketRef child = CFSocketCreateWithNative(NULL,
*(CFSocketNativeHandle *)data, kCFSocketDataCallBack, receiveData, NULL);
CFRunLoopSourceRef childSource = CFSocketCreateRunLoopSource(NULL,
child, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, childSource, kCFRunLoopDefaultMode);
CFRelease(childSource);
}
int main (int argc, const char *argv[]) {
struct sockaddr_in a = {0, AF_INET, 1234, 0};
CFDataRef d = CFDataCreate(NULL, (UInt8 *)&a, sizeof(struct
sockaddr_in));
CFSocketSignature signature = {PF_INET, SOCK_STREAM, IPPROTO_TCP, d};
CFSocketRef socket = CFSocketCreateWithSocketSignature(NULL,
&signature, kCFSocketAcceptCallBack, acceptConnection, NULL);
CFRunLoopSourceRef source = CFSocketCreateRunLoopSource(NULL,
socket, 0);
CFRunLoopRef loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode);
CFRunLoopRun();
}
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.
References: | |
| >CFSocket (From: Matt Jarjoura <email@hidden>) |