我正试图围绕目标 c tcp 套接字连接到服务器,我使用了这两个教程中的代码:https ://www.raywenderlich.com/3932/networking-tutorial-for-ios-how -to-create-a-socket-based-iphone-app-and-server和https://gist.github.com/rjungemann/446256。我注意到,当我尝试将数据发送出去时,它不会发送,直到我停止应用程序(此时在服务器上接收到),遵循两个教程时的相同行为,下面的相关代码:
- (void)setup {
NSURL *url = [NSURL URLWithString:host];
NSLog(@"Setting up connection to %@ : %i", [url absoluteString], port);
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (__bridge CFStringRef)[url host], port, &readStream, &writeStream);
if(!CFWriteStreamOpen(writeStream)) {
NSLog(@"Error, writeStream not open");
return;
}
[self open];
NSLog(@"Status of outputStream: %lu", (unsigned long)[outputStream streamStatus]);
return;
}
- (void)writeOut:(NSString *)s {
uint8_t *buf = (uint8_t *)[s UTF8String];
[outputStream write:buf maxLength:strlen((char *)buf)];
NSLog(@"Writing out the following:");
NSLog(@"%@", s);
}