我正在编写一个处理服务器端和客户端的 iPhone 应用程序。我遇到的问题是我无法将任何数据从我的应用程序(客户端)发送到我的 java 程序(服务器端)。我现在有点卡住了,任何帮助将不胜感激。
我的 obj-c 代码(客户端):
- (BOOL) initConnection: (NSString *) ipAddr {
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)ipAddr, 4444, NULL, &writeStream);
if(!writeStream)
return NO;
outStream = (__bridge NSOutputStream *) writeStream;
[outStream setDelegate:self];
[outStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[outStream open];
self.data = [[NSData alloc] init];
return YES;
- (void) sendCommand: (NSString *) command {
NSLog(@"Command that was sent to method: %@", command);
self.data = [NSData dataWithBytes:@"This is a test\n" length:16];
int bytes = [outStream write:[self.data bytes] maxLength:[self.data length]];
我的 Java 代码(服务器):
private void handleCommands() {
try {
do {
System.out.println("Right before reading socket");
msg = in.readLine();
System.out.println("Received: " + msg);
} while(!EOC);
} catch(IOException e) {
e.printStackTrace();
}
}
发生的情况是,当我从应用程序发送数据时,服务器端的代码永远不会超过
msg = in.readLine()。当我在应用程序端终止连接时,它会打印出收到的字符串为null。