I'm trying to let a (android) client communicate with a server. Sending data client -> server works fine, but I want the server to respond. Client side code is:
try { Socket s = new Socket("192.168.0.36", 12390); s.setSoTimeout(5000); JSONObject json = new JSONObject(); json.put("emergency", false); json.put("imei", imei); json.put("lat", l.getLatitude()); json.put("lon", l.getLongitude()); json.put("acc", l.getAccuracy()); json.put("time", l.getTime()); BufferedWriter out = new BufferedWriter(new OutputStreamWriter( s.getOutputStream())); out.write(json.toString()); out.newLine(); out.write("###"); out.flush(); Log.d(TAG, "sent"); String inputLine = null; String result = ""; BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); Log.d(TAG, "open input"); while ((inputLine = in.readLine()) != null) { Log.d(TAG, "while"); if (inputLine.contains("###")) { break; } result = result.concat(inputLine); } Log.d(TAG, "closing socket"); s.close(); Server side is:
Socket c = null; while (true) { try { c = s.accept(); } catch (IOException e) { System.out.println("Accept failed: " + port); System.exit(-1); } try { BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(c.getOutputStream())); String inputLine = null; String result = ""; while ((inputLine = in.readLine()) != null) { if (inputLine.contains("###")) { System.out.println("received ###"); out.write("Hello phone"); out.newLine(); out.write("###"); out.newLine(); out.flush(); } result = result.concat(inputLine); } System.out.println(result); out.close(); The server reads the message from the client correctly and after receiving ### should be sending back a message, but that is never received by the client. The client times out on
while ((inputLine = in.readLine()) != null) { Log.d(TAG, "while"); if (inputLine.contains("###")) { break; } and never enters the while loop. What am I missing here? Any ideas appreciated!