I created server in Objective C, using CocoaAsyncTask
, it works fine if I connect from telnet both localhost and on same network.
Now I have a client in Java, that works fine with another Java server and I cannot get to communicate; no messages are received on sever side.
For reference, here is the Java Client code:
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("192.168.0.106",4353);
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("Enter message: ");
String msg = br.readLine();
if (msg != null && !msg.equals("bye"))
{
printWriter.println(msg);
}
else
{
break;
} } } catch (IOException e) {e.printStackTrace();} } }
And the server Objective C code:
...
[newSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:15.0 tag:0];
...
(void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag {
dispatch_async(dispatch_get_main_queue(), ^{
@autoreleasepool {
DDLogInfo(@"data: %@", data);
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)];
DDLogInfo(@"Info: %@", strData);
NSString *msg = [[NSString alloc] initWithData:strData encoding:NSUTF8StringEncoding];
DDLogInfo(@"Info: %@", msg);
We've tried appending \\n
and \\r
at the end, but no success.
What could cause this miss-communication?
The problem was on the server side, by receiving the data from socket using readDataToData
, instead of readDataWithTimeout
.
Most probable, the problem is in line ending, so the library does not process it as a line, thus not calling the method for the next data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.