this is my code in ViewController.h in CoCoa to implement "CFsocket"
@interface ViewController : NSViewController
-(IBAction)start:(id)sender;
@property (strong, nonatomic) IBOutlet NSTextView *CommandDisplay;
this is ViewController.m
@implementation ViewController
@synthesize CommandDisplay=_CommandDisplay;
void AcceptCallBack(CFSocketRef socket,CFSocketCallBackType type,CFDataRef address,const void *data,void *info)
{
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
// For a kCFSocketConnectCallBack that failed in the background, it is a pointer to an SInt32 error code; for a kCFSocketAcceptCallBack, it is a pointer to a CFSocketNativeHandle; or for a kCFSocketDataCallBack, it is a CFData object containing the incoming data. In all other cases, it is NULL.
CFSocketNativeHandle sock = *(CFSocketNativeHandle *) data;
CFStreamCreatePairWithSocket(kCFAllocatorDefault, sock, &readStream, &writeStream);
if (!readStream || !writeStream)
{
close(sock);
NSLog(@"CFStreamCreatePairWithSocket()Fail");
return;
}
CFStreamClientContext streamCtxt = {0, NULL, NULL, NULL, NULL};
CFReadStreamSetClient(readStream, kCFStreamEventHasBytesAvailable, ReadStreamClientCallBack, &streamCtxt);
CFWriteStreamSetClient(writeStream, kCFStreamEventCanAcceptBytes, WriteStreamClientCallBack, &streamCtxt);
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
CFWriteStreamScheduleWithRunLoop(writeStream, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
CFReadStreamOpen(readStream);
CFWriteStreamOpen(writeStream);
}
// readstream operatoion , use when client transmitted data
void ReadStreamClientCallBack(CFReadStreamRef stream, CFStreamEventType eventType, void* clientCallBackInfo)
{
UInt8 buff[255];
CFReadStreamRef inputStream = stream;
CFReadStreamRead(stream, buff, 255);
_CommandDisplay.string=[self._CommandDisplay.string stringByAppendingString:[NSString stringWithFormat:@"SeverCreat failed\n"]];
NSLog(@"receive: %s",buff);
NSLog(@"%@",clientCallBackInfo);
CFReadStreamClose(inputStream);
CFReadStreamUnscheduleFromRunLoop(inputStream,CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
inputStream = NULL;
}
when i use c function , it can't recognize _CommandDisplay which i have synthesize ,but i need to print read data to NSTextView,how can i solve this problem?
In Objective-C a synthesized property
foois backed by an implicit instance variable_foo.If you want to access the instance variable directly use
_foowithoutself.If you want to access the property by its synthesized getter and setter use
self.foo(without the underscore)Write
or
NSString stringWithFormatis not needed, there are no format parameters, and you can also delete the@synthesizeline, it's not needed either.A small side-note:
If the C-function was outside the scope of the implementation block, you would have to pass the reference to the NSTextView instance thru the
infoparameter of the function, but in this case it should work