Re: Asynchronous Socket Code
Re: Asynchronous Socket Code
- Subject: Re: Asynchronous Socket Code
- From: Dan Watson <email@hidden>
- Date: Mon, 09 Jul 2001 19:58:52 -0400
>
> I wrote my own little bsd socket wrapper class to do just this. it
>
> basically
>
> detaches a thread to poll for data, and calls dataAvailable when it gets
>
> some. you just need to subclass it and override the event functions
>
> (connected, disconnected, dataAvailable, error) to do what you want.
>
>
>
> I'm not sure how efficient this approach is, and I haven't looked into
>
> CFSockets yet, but this works well enough for me :)
>
>
>
> I've posted an archive of what I've written. it's uncommented, but
>
> should be
>
> fairly self explanitory. feel free to use and modify. it's at:
>
Hello, I looked over your class and it is exactly what I need (from
>
what I get)
>
>
I am EXTREMELY need to Obj-C and Cocoa. I would really appreciate it if
>
you could send me an example on how it works. It doesn't have to be
>
commented or anything. Just how you USE it.
>
>
>
Thanks,
>
>
Graham
you would do something like this:
--- MySocket.h ---
#import <Cocoa/Cocoa.h>
@interface MySocket : TCPSocket
{
}
- (void)connected;
- (void)disconnected;
- (void)dataAvailable;
@end
--- MySocket.m ---
#import "MySocket.h"
@implementation MySocket
- (void)connected
{
NSLog( @"socket connected, lets send something" );
[self writeString:@"hello world"];
}
- (void)disconnected
{
NSLog( @"socket disconnected" );
}
- (void)dataAvailable
{
NSLog( @"data available on socket, buffer = %@", [buffer description] );
}
@end
then you could do something like this when you want to connect.
- (void)myConnectFunction
{
MySocket *sock = [[MySocket alloc] init];
NSString *addr = @"server.com";
unsigned short port = 1234;
[sock connectToHost:addr port:port];
}
hope this helps. also, there might be a few errors, as I typed this in as I
went.
cheers,
dan
----------------------------------------------------------------------------
dan watson
email@hidden