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: Jeremy Wyld <email@hidden>
- Date: Tue, 9 Nov 2004 09:32:49 -0800
Before calling close, you should make sure to remove your client and
unschedule from the run loop. This will guarantee you won't get called
back ever again, and there will be no references to your stream in the
run loop.
jeremy
On Nov 9, 2004, at 6:29 AM, Suraj Rai wrote:
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:
40mac.com
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
_______________________________________________
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