objective-c下,cocatouch框架把原生的socket做了进一步的封装,也就是stream.
添加CFNetwork框架
初始化套接字
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"127.0.0.1", 7777, &readStream, &writeStream);
inputStream = ( NSInputStream *)readStream;
outputStream = ( NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];
消息处理
- (void)stream:(NSStream *)theStream handleEvent:(N