【问题标题】:Java server- client socket communicationJava 服务器-客户端套接字通信
【发布时间】:2012-07-26 08:30:19
【问题描述】:

我正在尝试让(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循环。我在这里想念什么?任何想法表示赞赏!

【问题讨论】:

    标签: java sockets client


    【解决方案1】:

    来自客户:

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

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

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

    所以,我相信您的服务器无法接收此消息。试着放

       out.newLine();
    

    发送“###”之后。

    【讨论】:

    • 原来如此!我没有注意它,因为服务器记录了“System.out.println("received ###");”,但是那个解决了它。非常感谢!
    【解决方案2】:

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

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

    就在旁边

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

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

    【讨论】:

      猜你喜欢
      • 2017-08-23
      • 1970-01-01
      • 2012-08-30
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多