CFReadStreamRef _stream;
CFHTTPMessageRef _request;
CFHTTPMessageRef _response;
int main(int argc, char* argv[])
{
printf("hello,
xcode\n");
CFURLRef url = "" CFSTR("
http://192.168.0.58"), NULL);
if(url ==
NULL) {
printf("error: create url\n");
return
-1;
}
_request = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
CFSTR("GET"), url,
kCFHTTPVersion1_1);
CFRelease(url);
if(_request == NULL)
{
printf("error: create request\n");
return
-1;
}
_stream =
CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault,
_request);
CFRelease(_request);
if(_stream == NULL)
{
printf("error: create stream for
request\n");
return -1;
}
// CFStreamClientContext is used for callback function to receive
user-defined data.
// CFStreamClientContext ctxt = {0,
NULL, NULL, NULL, NULL};
// Set client: call back function
// if(!
CFReadStreamSetClient(_stream, kNetworkEvents, readStreamClientCallBack,
&ctxt)) {
if(! CFReadStreamSetClient(_stream, kNetworkEvents,
readStreamClientCallBack, NULL))
{
CFRelease(_stream);
printf("error: set client to
read stream\n");
return -1;
}
// Schedule the
stream
CFReadStreamScheduleWithRunLoop(_stream, CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
// Start the HTTP connection
if(! CFReadStreamOpen(_stream))
{
CFReadStreamSetClient(_stream, 0, NULL,
NULL);
CFReadStreamUnscheduleFromRunLoop(_stream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
CFRelease(_stream);
printf("error:
open stream to send http request");
return
-1;
}
printf("exit main entry...");
return
0;
}
Thanks,
Alex Guo