IOS sends data using socket

I implemented a socket connection in iOS.

What I want to do is send a data string to a connected device ... (I can receive data when someone sends it to my device) I tried this code, but the data is received on another device when I close my application.

- (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]]; } 

Where am I making a mistake?

+5
source share
2 answers

After hard research, I found that adding \n at the end of the string message indicates that my buffer received the whole string. now start sending the file to make it work ...

To send String over Sockets, you must mark the end of the string. i.e; Add \n

+3
source

To mark a line with the beginning and end of a line, for this you need ASCII control characters , octets \002 and \003 respectively. I would recommend using these control characters instead of just adding line breaks, your text may have line breaks and this can cause problems.

0
source

All Articles