How to send NSString via socket using NSOutputStream

I need to create a chat application for iOS using socket programming, and my IP address is 192.168.0.57:9300 . I used the Raywenderlich socket programming example, getting data working properly, but sending does not work, there are no errors or failures. My code is as follows.

code for opening threads

 - (void) initNetworkCommunication { CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.0.57", 9300, &readStream, &writeStream); inputStream = (NSInputStream *)readStream; outputStream = (NSOutputStream *)writeStream; [inputStream setDelegate:self]; [outputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; [outputStream open]; } 

code for sending data

 - (IBAction)sendMessage:(id)sender { NSString *response = @"lets start chat"; NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; [outputStream write:[data bytes] maxLength:[data length]]; } 

Delegates

 - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent { NSLog(@"stream event %i", streamEvent); switch (streamEvent) { case NSStreamEventOpenCompleted: NSLog(@"Stream opened"); break; case NSStreamEventHasBytesAvailable: 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(@"\nreciving data------%@,buffer); [self messageReceived:output]; } } } } break; case NSStreamEventErrorOccurred: NSLog(@"Can not connect to the host!"); break; case NSStreamEventEndEncountered: [theStream close]; [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [theStream release]; theStream = nil; break; default: NSLog(@"Unknown event"); } } Message sending - (void) messageReceived:(NSString *)message { [self.messages addObject:message]; [self.tView reloadData]; NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:messages.count-1 inSection:0]; [self.tView scrollToRowAtIndexPath:topIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; } 

please provide me an offer.

+8
ios iphone sockets
source share
3 answers

You should add "\ n" at the end of your answer as follows:

 - (IBAction)sendMessage:(id)sender { NSString *response = @"lets start chat\n"; ////your code } 

This work for me, but my problem is that I cannot receive data using the function (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent

+5
source share

I had a similar problem. I got this solution by adding a newline and line character to the line.

 - (IBAction)sendMessage:(id)sender { NSString *response = @"lets start chat\r\n\r\n"; NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; [outputStream write:[data bytes] maxLength:[data length]]; } 
+1
source share

I found that for writing a separate thread this problem was solved for me.
It makes sense, since you do not need to do network operators in the main thread.
Here is my code:

 dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^(void) { NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]]; NSInteger len = [outputStream write:[data bytes] maxLength:[data length]]; NSLog(@"Len = %ld", len); }); 

One more note: the NSStreamEventOpenCompleted event is fired twice. Once, when each of the input and output streams opens. Therefore, you need to be careful not to write to the output stream before its event.

+1
source share

All Articles