【问题标题】:Java two- way socket connection (server/ client)Java双向套接字连接(服务器/客户端)
【发布时间】:2012-07-24 02:32:41
【问题描述】:

我正在尝试将一些 JSON 从 Android 手机发送到 Java 服务器,这可以正常工作。 Android/客户端是这样的:

                    Socket s = new Socket("192.168.0.36", 12390);
                s.setSoTimeout(1500);

                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.flush();
                s.close();

服务器端是这样的:

        try {
        s = new ServerSocket(port);
    } 
    catch (IOException e) {
        System.out.println("Could not listen on port: " + port);
        System.exit(-1);
    }


    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()));
                String inputLine = null;
            String result = "";
            while ((inputLine = in.readLine()) != null) {
                result = result.concat(inputLine);  
            }
            System.out.println(result);

正如我所说,所有这些都有效。现在,我想在收到来自客户端的消息后,将消息从服​​务器发送回客户端。 我扩展了这样的代码,Android/客户端:

                    Socket s = new Socket("192.168.0.36", 12390);
                s.setSoTimeout(1500);

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

                String inputLine = null;
                String result = "";
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));  
                while ((inputLine = in.readLine()) != null) {
                    Log.d(TAG, in.readLine());
                    result = result.concat(inputLine);
                }

和服务器端:

        try {
    s = new ServerSocket(port);
} 
catch (IOException e) {
    System.out.println("Could not listen on port: " + port);
    System.exit(-1);
}


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()));
            String inputLine = null;
        String result = "";
        while ((inputLine = in.readLine()) != null) {
            result = result.concat(inputLine);  
        }
        System.out.println(result);

    PrintWriter out = new PrintWriter(c.getOutputStream());
    out.write("Hello phone");
    out.flush();
    out.close();

在客户端,什么都没有进来,它挂在上面

                while ((inputLine = in.readLine()) != null) {
                Log.d(TAG, in.readLine());
                result = result.concat(inputLine);
            }

直到套接字超时(从不进入循环)。我认为这可能是一个时间问题,例如服务器太早发送它的回复,因此客户端从来没有收到任何东西,但我试图把 out.write("Hello phone");代码中的几乎任何地方,总是相同的结果。它与从 ServerSocket 获得的套接字并且无法发送数据有关吗?我在这里错过了什么,这整天困扰着我......

编辑:在 Nikolais 回答后,我尝试了这个(客户端):

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


                String inputLine = null;
                String result = "";
                BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));  
                while ((inputLine = in.readLine()) != null) {
                    if (inputLine.contains("###")) {
                        break;
                    }
                    Log.d(TAG, in.readLine());
                    result = result.concat(inputLine);

                }


                s.close();

和服务器:

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

                result = result.concat(inputLine);  
                if (inputLine.contains("###")) {
                    System.out.println("received ###");
                    out.println("Hello phone");
                    out.println("###");
                    out.flush();
                    break;
                }

            }

这个想法是在客户端关闭套接字之前从服务器发送消息。还是不行……有什么提示吗?

【问题讨论】:

    标签: java sockets client


    【解决方案1】:

    在服务器端,您永远无法发送“Hello phone”。直到客户端关闭套接字,但那时它是无用的。这是因为in.readLine() 阻塞,直到数据可用或EOF,即套接字关闭。

    您需要一种摆脱阅读循环的方法 - 发明(或采用)一些应用程序级协议,该协议会告诉您已收到整条消息。常用选项有定长消息、长度前缀、定界等。

    【讨论】:

    • 这绝对有道理,谢谢。也许你可以看看我编辑的问题,我想这大致是你的意思?
    • @user804967 - 在您的特殊代码后发送换行符,否则另一端的readLine() 不会返回。
    • 是的,我错过了。在“Hello phone”和“###”之后放置一个 newline(),仍然是同样的问题 :( 服务器识别 ### String btw,但客户端仍然没有进入读取循环。跨度>
    • 在客户端,您使用 Log.d(TAG, in.readLine()); 多消耗一行 - 这会占用您的 ### 标记。
    • 好主意,我删除了该行,但我从未进入该循环。如果那条线消耗了任何东西,它就会出现在 logcat 中。很抱歉打扰你们,但你们有想法,而我完全没有想法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多