1

我试图让(android)客户端与服务器通信。发送数据客户端 - > 服务器工作正常,但我希望服务器响应。客户端代码是:

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

服务器端是:

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

服务器正确地从客户端读取消息,并且在接收到### 后应该发回一条消息,但客户端永远不会收到该消息。客户端超时

                    while ((inputLine = in.readLine()) != null) {
                    Log.d(TAG, "while");
                    if (inputLine.contains("###")) {
                        break;
                    }

并且永远不会进入while循环。我在这里想念什么?任何想法表示赞赏!

4

2 回答 2

2

来自客户:

out.write(json.toString());
out.newLine();
out.write("###");
out.flush();

您忘记在发送“###”后添加新行,因为您的服务器使用 readLine()

 while ((inputLine = in.readLine()) != null) {
 //...
 }

因此,我相信您的服务器无法收到此消息。试着放

   out.newLine();

发送“###”后。

于 2012-07-26T09:29:00.473 回答
0

我在这里可能错了,但根据我的经验,我总是在开始通讯之前打开输入和输出流。尝试移动您的

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));  

旁边

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

以防您的数据包在您打开输入流并开始读取它们之前过期。

于 2012-07-26T08:55:15.857 回答