2016-09-20 68 views
5

我在iOS中實現了套接字連接。iOS使用套接字連接發送數據

我想要做的是將數據串發送到連接的設備... (我能夠接收數據時,有人發送到我的設備) 我試過這個代碼,但數據越來越接收其他設備,當我關閉我的應用程序。

- (IBAction)connectToServer:(id)sender { 

    NSLog(@"Setting up connection to %@ : %i", _ipAddressText.text, [_portText.text intValue]); 
    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef) _ipAddressText.text, [_portText.text intValue], &readStream, &writeStream); 

    messages = [[NSMutableArray alloc] init]; 

    [self open]; 
} 

- (void)open { 

    NSLog(@"Opening streams."); 

    outputStream = (__bridge NSOutputStream *)writeStream; 
    inputStream = (__bridge NSInputStream *)readStream; 

    [outputStream setDelegate:self]; 
    [inputStream setDelegate:self]; 

    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 

    [outputStream open]; 
    [inputStream open]; 

    _connectedLabel.text = @"Connected"; 
} 

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { 

    NSLog(@"stream event %lu", streamEvent); 

    switch (streamEvent) { 

     case NSStreamEventOpenCompleted: 
      NSLog(@"Stream opened"); 
      _connectedLabel.text = @"Connected"; 
      break; 

     case NSStreamEventHasBytesAvailable: 
      if (theStream == inputStream) 
      { 
       uint8_t buffer[1024]; 
       NSInteger 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 messageReceived:output]; 
         } 
        } 
       } 
      } 
      break; 

     case NSStreamEventHasSpaceAvailable: 
      NSLog(@"Stream has space available now"); 
      break; 

     case NSStreamEventErrorOccurred: 
      NSLog(@"error: %@",[theStream streamError].localizedDescription); 
      break; 

     case NSStreamEventEndEncountered: 
      [theStream close]; 
      [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
      _connectedLabel.text = @"Disconnected"; 
      NSLog(@"close stream"); 
      break; 

     default: 
      NSLog(@"Unknown event"); 
    } 
} 

/* Sends data to other device */ 
- (IBAction) sendMessage { 

    NSLog(@"sendMessage"); 
    NSString *response = [NSString stringWithFormat:@"msg:%@", _dataToSendText.text]; 
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; 
    NSLog(@"[data length] %lu",(unsigned long)[data length]); 
    [outputStream write:[data bytes] maxLength:[data length]]; 

} 

我在哪裏犯錯?

回答

3

經過艱苦的研究後,我發現在字符串消息的末尾添加\n表示我的緩衝區已收到整個字符串。 現在開始發送一個文件,所以它的工作...

要發送字符串套接字,您必須標記字符串的結尾。即;添加\n

0

爲了紀念與開始和結束字符串串有ASCII control characters這麼做中,八位字節分別爲\002 & \003。我會建議使用這些控制字符,而不是僅添加換行符,您的文本可能有換行符,這可能會產生問題。