//调用此接口与socket连接
- (void)connect{if (DV_CONNECTION_CONNECTED ==_connectStatus)return; CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.10.1.1", 8150, &_readStream, &_writeStream); inputStream= (NSInputStream *)_readStream; //ivar [inputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; outputStream= (NSOutputStream *)_writeStream; //ivar [outputStream setDelegate:self]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream open]; _connectStatus=DV_CONNECTION_CONNECTING; } //调用此接口与socket断开连接- (void)disConnect{ _connectStatus=DV_CONNECTION_DISCONNECTED;if(inputStream){ [inputStream close]; inputStream=nil; }if(outputStream){ [outputStream close]; outputStream=nil; } [self notifyDisConnected]; } //获取socket的连接状态- (int)connectStatus{return_connectStatus; } //注册连接状态的Delegate- (void)registerDVConnectionChangedDelegate:(id)delegate{if (nil == _dvConnectionChangedDelegates || nil == delegate)return;if (DV_CONNECTION_CONNECTED ==[self connectStatus]) [delegateonDVConnected];else if (DV_CONNECTION_DISCONNECTED ==[self connectStatus]) [delegateonDVDisConnected];else if (DV_CONNECTION_CONNECT_FAILED ==[self connectStatus]) [delegateonConnectedFailed]; [_dvConnectionChangedDelegates addObject:delegate]; } //反注册连接状态的Delegate- (void)unregisterDVConnectionChangedDelegate:(id)delegate{if (nil == _dvConnectionChangedDelegates || nil == delegate)return; [_dvConnectionChangedDelegates removeObject:delegate]; } //发送命令给Server- (BOOL)sendCommand:(NSString *)string{while (true) {if([outputStream hasSpaceAvailable]){ NSData*data = [[NSData alloc] initWithData:[stringdataUsingEncoding:NSASCIIStringEncoding]]; [outputStream write:[data bytes] maxLength:[data length]]; NSLog(@"write command to dv: %@", string);returnYES; } }returnNO; } //Socket回调,可以知道Socket的连接状态,收到Server端发来的数据等- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {switch(streamEvent) {caseNSStreamEventNone: [self notifyConnectedFailed]; NSLog(@"can not connect to socket");break;caseNSStreamEventOpenCompleted:if (theStream ==inputStream) { [self notifyConnected]; } NSLog(@"Stream opened");break;caseNSStreamEventHasSpaceAvailable: {break; }caseNSStreamEventHasBytesAvailable:if (theStream ==inputStream) { uint8_t buffer[1024];intlen;while([inputStream hasBytesAvailable]) { len= [inputStream read:buffer maxLength:sizeof(buffer)];if (len > 0) {//一行作位一次会话传给外边解析Iterator buff. uint8_t tmp_buffer[1024];int tmp_buffer_start = 0;for (int i = 0; i < len; ++i) {if ('\n' ==buffer[i]) {for (int j = tmp_buffer_start; j < i + 1; ++j) { tmp_buffer[j- tmp_buffer_start] =buffer[j]; } NSString*output = [[NSString alloc] initWithBytes:tmp_buffer length:(i - tmp_buffer_start + 1) encoding:NSASCIIStringEncoding];if (nil !=output) { [self notifyNewMessage:output]; NSLog(@"s: %@", output); } [output release]; memset(tmp_buffer,0, sizeof(uint8_t) * 1024); tmp_buffer_start= i + 1; } } } } }break;caseNSStreamEventErrorOccurred: NSLog(@"Can not connect to the host!");//[self notifyConnectedFailed]; break;caseNSStreamEventEndEncountered: NSLog(@"Closing stream..."); [theStream close]; [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [theStream release]; theStream=nil;break;default: NSLog(@"Unknown event"); } }