android 和ios socket server,Android Socket 与 IOS Socket

//调用此接口与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"); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值