2013-12-10 166 views
0

我第一次編寫IOS應用程序。它應該連接到一個靜態IP設備,併發送某些「已知」命令給它。但由於某種原因我無法建立連接。無法通過IOS建立Socket連接

波紋管是我用來建立連接並將數據寫入端口的函數。

-(void)connection//:(NSString *)serviceName forIpAddress:(NSString *)ipAddress forPort:(NSString *)portNo 
{ 
    if(input && output) 
     [self close]; 
    NSString *urlString = [NSString stringWithFormat:@"%.%.%.%", "192.168.3.120"]; 
    NSURL *website = [NSURL URLWithString:urlString]; 
    if (!website) { 
     NSLog(@"%@ is not a valid URL", website); 
    } 

    CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)[website host], 43, &readStream, &writeStream); 
    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); 
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); 
    NSInputStream *input = (__bridge NSInputStream *)readStream; 
    NSOutputStream *output= (__bridge NSOutputStream *)writeStream; 
} 



    - (void)open { 
    [input setDelegate:self]; 
    [output setDelegate:self]; 
    [input scheduleInRunLoop:[NSRunLoop currentRunLoop] 
        forMode:NSDefaultRunLoopMode]; 
    [output scheduleInRunLoop:[NSRunLoop currentRunLoop] 
         forMode:NSDefaultRunLoopMode]; [input open]; 
    [output open]; 
} 




-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent 
{ 
    NSString *event; 
    switch (streamEvent) 
    { 
     case NSStreamEventNone: 
      event = @"NSStreamEventNone"; 
      break; 
     case NSStreamEventOpenCompleted: 
      event = @"NSStreamEventOpenCompleted"; 
      break; 
     case NSStreamEventHasBytesAvailable: 
      event = @"NSStreamEventHasBytesAvailable"; 
      if (theStream == input) 
      { 
       uint8_t buffer[1024]; 
       NSInteger len; 
       while ([input hasBytesAvailable]) 
       { 
        len = [input read:buffer maxLength:1024]; 
        if (len > 0) 
        { 
          NSMutableString *output = [[NSMutableString alloc]initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];              NSLog(@"Received data--------------------%@", output); 
        } 
       } 
      } 
             break; 
             case NSStreamEventHasSpaceAvailable: 
             event = @"NSStreamEventHasSpaceAvailable"; 
             break; 
             case NSStreamEventErrorOccurred: 
             event = @"NSStreamEventErrorOccurred"; 
             //[self close]; 
             break; 
             case NSStreamEventEndEncountered: 
             break; default: 
             event = @"NSStreamEventEndEncountered"; 
             //[self close]; 
             event = @"Unknown"; break; 
    } 
    NSLog(@"event------%@",event); 
} 



- (void)close 
{ 
    [input close]; 
    [output close]; 
    [input removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode]; 
    [output removeFromRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode]; 
    [input setDelegate:nil]; 
    [output setDelegate:nil]; 
    input = nil; 
    output = nil; 
} 




- (void)dataSending:(NSString*)data 
{ 
    if(output) 
    { 
     if(![output hasSpaceAvailable]) 
      return; 
     NSData *_data=[data dataUsingEncoding:NSUTF8StringEncoding]; 
     NSInteger data_len = [_data length]; 
     uint8_t *readBytes = (uint8_t *)[_data bytes]; 
     int byteIndex=0; 
     unsigned int len=0; 
     while (TRUE) 
     { 
      len = ((data_len - byteIndex >= 40960) ? 40960 : (data_len-byteIndex)); 
      if(len==0) 
       break; 
      uint8_t buf[len]; 
      (void)memcpy(buf, readBytes, len); 
      len = [output write:(const uint8_t *)buf maxLength:len]; 
      byteIndex += len; 
      readBytes += len; 
     } 
     NSLog(@"Sent data----------------------%@",data); 
    } 
} 

我通過代碼測試呼叫提到的功能,並沒有發生提前

- (IBAction)pumpchange:(id)sender { 
    [self connection]; 
    [self open]; 

    if ([self.pump backgroundImageForState:(UIControlStateNormal)]==[UIImage imageNamed:@"PumpOff.png"]) 
    { 
     [self.pump setBackgroundImage:[UIImage imageNamed:@"PumpOn.png"] forState:(UIControlStateNormal)]; 
     [self dataSending:@"pump_on"]; 
    } 

    else //if ([self.pump backgroundImageForState:(UIControlStateNormal)]==[UIImage imageNamed:@"PumpOn.png"]) 
    { 
     [self.pump setBackgroundImage:[UIImage imageNamed:@"PumpOff.png"] forState:(UIControlStateNormal)]; 
     [self dataSending:@"pump_off"]; 
    } 
    [self close]; 
} 

感謝

回答

3

似乎有一些誤解格式字符串是如何工作的,因爲

NSString *urlString = [NSString stringWithFormat:@"%.%.%.%", "192.168.3.120"]; 

只是給你字符串@"..."。也許你的意思

NSString *urlString = [NSString stringWithFormat:@"%d.%d.%d.%d", 192, 168, 3, 120]; 

NSString *urlString = [NSString stringWithFormat:@"%s", "192.168.3.120"]; 

但你並不需要一個格式字符串都:

NSString *urlString = @"192.168.3.120";