Re: Connecting a client to a tcp server?
Re: Connecting a client to a tcp server?
- Subject: Re: Connecting a client to a tcp server?
- From: "Michael Purdy" <email@hidden>
- Date: Tue, 09 Nov 2004 13:20:40 -0500
Sorry about this question if it is rather basic or seemingly
uninformed... In looking at your example code, I believe it is strictly
reading data coming in from a server. Is this true? In the case of
sending data to the server, is the corresponding set of
"CFWriteStream<x>" functions the way to go?
Michael
>>> Suraj Rai <email@hidden> 11/9/2004 9:29:54 AM >>>
Hi Michael,
I am also a beginner with CF Networks and I recently wrote a small test
program to do just this. Perhaps this will be of use to you:
#include <CoreFoundation/CoreFoundation.h>
void myCallBack (CFReadStreamRef stream, CFStreamEventType event, void
*myPtr) {
switch(event) {
case kCFStreamEventHasBytesAvailable: {
UInt8 buf[1024];
CFIndex bytesRead = CFReadStreamRead(stream, buf, 1024);
if (bytesRead > 0) {
// write code here to deal with buf
}
break;
}
case kCFStreamEventErrorOccurred: {
CFStreamError error = CFReadStreamGetError(stream);
fprintf(stderr, "Error in callback\n");
CFReadStreamClose(stream);
CFRelease(stream);
break;
}
case kCFStreamEventEndEncountered: {
fprintf(stderr, "Error in callback\n");
CFReadStreamClose(stream);
CFRelease(stream);
stream = NULL;
break;
}
}
}
int main (int argc, const char * argv[]) {
CFStringRef host = CFSTR("localhost");
UInt32 port = 9999;
CFReadStreamRef myReadStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host,
port,
&myReadStream, NULL);
CFStreamClientContext myContext = {0, NULL, NULL, NULL, NULL};
if (!CFReadStreamSetClient(myReadStream,
kCFStreamEventHasBytesAvailable |
kCFStreamEventErrorOccurred |
kCFStreamEventEndEncountered,
myCallBack, &myContext)) {
printf("This stream does not support stream-related events.
Exiting ...");
return 1;
} else {
CFReadStreamScheduleWithRunLoop(myReadStream,
CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
if (!CFReadStreamOpen(myReadStream)) {
CFStreamError myErr = CFReadStreamGetError(myReadStream);
if (myErr.error != 0) {
if (myErr.domain == kCFStreamErrorDomainPOSIX) {
strerror(myErr.error);
} else if (myErr.domain ==
kCFStreamErrorDomainMacOSStatus)
{
OSStatus macError = (OSStatus)myErr.error;
}
}
}
CFRunLoopRun();
return 0;
}
On Nov 9, 2004, at 11:16 PM, Michael Purdy wrote:
> I have no experience with CF Network APIs. I'm hoping someone can
help
> or give a quick example of some code to connect a c++ client via tcp
> to
> a server running on localhost port 8889.
>
> The server is a java app which accepts java clients fine. I would
like
> to base the flow of the c++ app on the working java clients.
>
> -connect
> -send data
> -wait for return data
> -then (upon a return stream completing) continue to the next lines
of
> code.
>
> The data being sent will be an array of floats. Any help or
guidance
> would be much appreciated.
>
> Michael
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Macnetworkprog mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> email@hidden
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden