我使用套接字创建了一个应用程序,我使用下面的代码连接到服务器:
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.10.121", 8080, &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];
我尝试使用以下代码向服务器发送消息:
NSString *stringName = [NSString stringWithFormat:@"%@, send message to server", userName];
// Encode
NSData *dataTake2 =[stringName dataUsingEncoding:NSUTF8StringEncoding];
// Convert to Base64 data
NSData *base64Data = [dataTake2 base64EncodedDataWithOptions:0];
[outputStream write:[base64Data bytes] maxLength:[base64Data length]];
但它没有成功。