Java服务器-客户端套接字通信 [英] Java server- client socket communication

查看:144
本文介绍了Java服务器-客户端套接字通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

服务器端是:

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;
                    }

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

and never enters the while loop. What am I missing here? Any ideas appreciated!

推荐答案

来自客户端:

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

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

You forgot adding a new line after send "###", because your server use readLine()

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

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

So, I believe your server can't receive this message. Try to put

   out.newLine();

发送"###"后.

这篇关于Java服务器-客户端套接字通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆