I need to communicate between a Java app (desktop) and an iOS app. I've decide开发者_JS百科d to use AsyncSocket
on the iPhone side (and I've gotten data sending to work). For this question, I'm just running the EchoServer
demo app on OSX 10.6.4. Source code is here.
Here's my Java code, boiled-down:
Socket echoSocket = new Socket("192.168.0.102", 8085);
PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
out.println("hello there!");
out.flush();
This works and the "hello there!" shows up on a TCP Monitor.
On the EchoServer
, the output is just
Accepted client 192.168.0.102:4960
So I know they're talking but I cannot get the data!
Edit: On the Java side (with more code), I do receive the Welcome to the "AsyncSocket Echo Server" message.
Edit: If I browse to the server it works perfectly, so the problem is in my Java code. What else do I need to do for a complete write to a socket correctly?
I think this is a good clue, taken from that demo's AppController.m
:
[sock readDataToData:[AsyncSocket CRLFData] withTimeout:READ_TIMEOUT tag:0];
Looks like it will read a line that ends with a CR and LF. So what you might need to do on the Java side is this:
out.print("hello, there\r\n");
Or probably the same:
out.println("hello, there\r");
精彩评论