gpt4 book ai didi

java - Java与Objective C之间的Socket连接

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:49 25 4
gpt4 key购买 nike

我正在尝试用 Java 编写一个服务器并使用 iPhone 应用程序与其进行通信( Objective-C )。连接成功,我可以将数据写入服务器,但无法从服务器读回数据。我收到一条通知,指出有数据要读取,但数据为空。

Java 代码主要取自 an Oracle example .

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;

out.println("Hello World");

while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
System.out.flush();

out.println("test");

if (inputLine.equals("Bye")) {
break;
}
}

out.close();
in.close();
socket.close();

以及 Objective C 客户端:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^(void) {
NSLog(@"staying alive!");
[[DABMultitaskingController sharedInstance] startTask];
}];

CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"10.0.1.189", 4444, &readStream, &writeStream);

NSInputStream *inputStream = (NSInputStream *)readStream;
NSOutputStream *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];

_dataToSend = [[NSData dataWithBytes:"This is a test" length:15] retain];

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
switch (streamEvent) {
case NSStreamEventHasBytesAvailable: {
NSLog(@"can read: %@", theStream);

uint8_t *buffer;
NSUInteger length;
[(NSInputStream *)theStream getBuffer:&buffer length:&length];
NSData *data = [NSData dataWithBytes:buffer length:length];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"bytes: %@", data);
NSLog(@"string: %@", string);

break;
}
case NSStreamEventHasSpaceAvailable: {
NSLog(@"can write: %@", theStream);

if (_dataToSend != nil) {
NSLog(@"write: %@", _dataToSend);
[(NSOutputStream *)theStream write:[_dataToSend bytes] maxLength:[_dataToSend length]];
[_dataToSend release];
_dataToSend = nil;
}

break;
}
case NSStreamEventOpenCompleted: {
NSLog(@"open complete: %@", theStream);

break;
}
case NSStreamEventErrorOccurred: {
NSLog(@"error occurred: %@", theStream);

break;
}
case NSStreamEventEndEncountered: {
NSLog(@"end encountered: %@", theStream);

break;
}
default:
break;
}
}

最佳答案

你永远不会发送换行符\n 到 java 端 BufferedReader 使用这些作为 readLine() 的分隔符;

_dataToSend = [[NSData dataWithBytes:"This is a test\n" length:16] retain];

关于java - Java与Objective C之间的Socket连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6165502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com