English 中文(简体)
无法将数据从 obj- c 客户端发送到 Java 服务器
原标题:Can t send data from a obj-c client to a java server

我正在写一个iPhone 应用程序, 处理服务器侧面和客户端。 我的问题在于我无法从我的应用程序( 用户侧面) 向我的 java 程序( 服务员侧面) 发送任何数据。 我现在有点卡住了, 任何帮助都会非常感激 。

我的boj-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
" length:16];

int bytes = [outStream write:[self.data bytes] maxLength:[self.data length]];

我的爪哇代码( 服务员) :

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

What happens is that when I send data from my app the code on my server side never gets past
msg = in.readLine(). When I terminate the connection on the app side it prints out that the string received was null.

问题回答

实际上,在这个代码线上:

self.data = [NSData dataWithBytes:@"This is a test
" length:16];

字符串长度为 15, 但您通过 16 作为参数。 也许它正在等待最后的字符?

尝试不要硬编码字符串长度, 例如 :

NSString *msgStr = @"This is a test
";
self.data = [NSData dataWithBytes:msgStr length:[msgStr length]];




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签