2016-06-07 58 views
0

我想使用套接字將iPhone連接到我的Linux機器。我在linux上運行服務器程序,並將socket保持在監聽模式,並嘗試從我的iphone發送字符串。但無法連接到linux機器。我嘗試過CFStream API。連接我使用的端口3000。我的代碼如下:iOS到Linux的連接使用套接字

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSLog(@"view did load"); 
    data = [[NSMutableData alloc] init]; 
    outputStream = [[NSOutputStream alloc] initToMemory]; 
    inputStream = [[NSInputStream alloc] init]; 
    [self initNetworkCommunication]; 

    [self sendString:@"Hello World\n"] 

} 
- (void)initNetworkCommunication { 

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.62",3000, &readStream, &writeStream); 

    inputStream = (__bridge NSInputStream *)(readStream); // ivar 
    [inputStream setDelegate:self]; 
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream open]; 

    outputStream = (__bridge NSOutputStream *)(writeStream); // ivar 
    [outputStream setDelegate:self]; 
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [outputStream open]; 
} 

-(void)sendString:(NSString *)string { 

    NSLog(@"data string:%@",string); 
    if(!CFWriteStreamOpen(writeStream)){ 
     NSLog(@"Error, writeStream not open"); 

    //[outputStream close]; 
    } 
    NSLog(@"Status of outputStream: %i", [outputStream streamStatus]); 

    NSData *data = [[NSData alloc] initWithData:[string dataUsingEncoding:NSASCIIStringEncoding]]; 

    [outputStream write:[data bytes] maxLength:[data length]]; 
} 
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { 

    NSLog(@"thestream:%@",theStream); 
    NSLog(@"stream event %lu", (unsigned long)streamEvent); 
    BOOL byteIndex = nil; 

    switch (streamEvent) { 
     case NSStreamEventOpenCompleted: 
      NSLog(@"Stream opened"); 
      break; 
     case NSStreamEventHasSpaceAvailable: { 
      uint8_t *readBytes = (uint8_t *)[data mutableBytes]; 
      readBytes += byteIndex; // ivar 
      int data_len = [data length]; 
      unsigned int len = ((data_len - byteIndex >= 1024) ? 1024 : (data_len - byteIndex)); 
      uint8_t buf [len]; 
      (void)memcpy(buf, readBytes, len); 
      len = [(NSOutputStream *)theStream write:(const uint8_t *)buf maxLength:len]; 
      NSLog(@"Sending buffer of len: %d", len); 
      byteIndex += len; 
      break; 
     } 
     case NSStreamEventHasBytesAvailable: 
      NSLog(@"the stream:%@, inputStream:%@",theStream,inputStream); 
      if (theStream == inputStream) { 
       uint8_t buffer[1024]; 
       int len; 

       while ([inputStream hasBytesAvailable]) { 
        len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
        if (len > 0) { 
         NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; 

         if (nil != output) { 
          NSLog(@"server said: %@", output); 
         } 
        } 
       } 
       [self sendString:@"Another Test"]; 
      } 
       break; 

     case NSStreamEventErrorOccurred: 
      theError = [theStream streamError]; 
      NSString * event = [[NSString alloc]initWithFormat:@"NSStreamEventErrorOccurred %@ ",theError]; 
      NSLog(@"Can not connect to the host!:%@",event); 
      break; 
//  case NSStreamEventEndEncountered: 
//   NSLog(@"Closing stream..."); 
//   [theStream close]; 
//   [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
//   //[theStream release]; 
//   theStream = nil; 
//   break; 
//  default: 

//   NSLog(@"Unknown event"); 

    } 
} 

+0

停留在CFStreamCreatePairWithSocketToHost(NULL,(CFStringRef)@ 「192.168.1.62」,3000,&readStream,&writeStream)方案;並進入無限循環 – Prajakta

回答

0

我得到了解決。我做了一些錯誤。考慮到兩條線在視圖中加載並在initNetworkCommunication中添加了兩行解決了問題糾正的代碼如下。

- (void)viewDidLoad { 

    [super viewDidLoad]; 

    NSLog(@"view did load"); 
    data = [[NSMutableData alloc] init]; 
    // outputStream = [[NSOutputStream alloc] initToMemory]; // Commented this two lines 
    // inputStream = [[NSInputStream alloc] init]; 
    [self initNetworkCommunication]; 

    [self sendString:@"Hello World\n"] 

} 
- (void)initNetworkCommunication { 



//My program stuck at CFStreamCreatePairWithSocketToHost. I declared this two objects globally but it didn't work then I declared it here now it works 
    CFReadStreamRef readStream ; 
    CFWriteStreamRef writeStream ; 

    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.62",3000, &readStream, &writeStream); 

    inputStream = (__bridge NSInputStream *)(readStream); // ivar 
    [inputStream setDelegate:self]; 
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream open]; 

    outputStream = (__bridge NSOutputStream *)(writeStream); // ivar 
    [outputStream setDelegate:self]; 
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [outputStream open]; 
}