• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: CFSocket
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.
  • Follow-Ups:
    • event driven vs blocking
      • From: Chris Silverberg <email@hidden>
References: 
 >CFSocket (From: Matt Jarjoura <email@hidden>)

  • Prev by Date: CFSocket & CFRunLoop
  • Next by Date: Re: CFSocket & CFRunLoop
  • Previous by thread: Re: CFSocket & CFRunLoop
  • Next by thread: event driven vs blocking
  • Index(es):
    • Date
    • Thread