简体   繁体   English

Java套接字客户端未接收到数据

[英]Java socket client not receiving data

I seem to be having a few issues working with sockets in Java. 我似乎在使用Java套接字时遇到一些问题。 The issue is that the client doesn't receive data and, when it does sometimes receive it, the data is delayed by a large amount of time. 问题在于客户端不接收数据,并且有时它确实接收数据时,数据会延迟大量时间。 The server receives all data from the client correctly and responds how I would like it to (at least from the output it does). 服务器正确地从客户端接收所有数据,并响应我的要求(至少从它的输出)。 Any help on this would be helpful. 任何对此的帮助将是有帮助的。

Server: 服务器:

try{
        ServerSocket server = new ServerSocket(6969);
        while (true){
            System.out.println("Listening on port 6969");
            Socket client = server.accept();
            System.out.println("Accepted");
            BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
            System.out.println("Got the input reader");

            //This is where the input data will be stored
            StringBuffer sb = new StringBuffer();
            while (input.ready()){
                System.out.println("Reading data");
                int cha = input.read();
                if ((cha < 0)){
                    break;
                }
                sb.append((char)cha);
            }

            System.out.println("Recived Data:" + sb.toString());
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            System.out.println(sb.toString().equalsIgnoreCase("INIT"));
            if (sb.toString().equalsIgnoreCase("INIT")){
                JSONObject jo = new JSONObject();
                jo.put("password", "Password123");
                jo.put("testString", "tESTsTRING");
                jo.put("intTest", 222);
                System.out.println("Sending data:" + jo.toString());
                output.write(jo.toString());
                output.flush();
            }

        }
    }catch (Exception e){
        System.out.println(e.getMessage());
    }

Client: 客户:

try{
        Socket connection = new Socket(host, 6969);
        System.out.println("Set up connection. Sending data and waiting for response");
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));

        output.write("INIT");
        output.flush();

        BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        System.out.println("Got the input reader: " + connection.getInputStream().available());

        //This is where the input data will be stored
        StringBuffer sb = new StringBuffer();
        System.out.println(input.readLine() + " :: " + input.ready());
        while (input.ready()){
            System.out.println("Reading data");
            int cha = input.read();
            if ((cha < 0)){
                break;
            }
            sb.append((char)cha);
        }

        System.out.println("Recived Data:" + sb.toString());

    }catch (Exception e){
        e.printStackTrace();
    }

The server console output: 服务器控制台输出:

Listening on port 6969
Accepted
Got the input reader
Reading data
Reading data
Reading data
Reading data
Recived Data:INIT
true
Sending data:{"intTest":222,"testString":"tESTsTRING","password":"Password123"}

The client output: 客户端输出:

Set up connection. Sending data and waiting for response
Got the input reader: 0

Also, the server sometimes doesn't receive the data from the client even though it recognizes that the client is trying to send the data. 同样,服务器有时即使不认识到客户端正在尝试发送数据,也无法从客户端接收数据。

If you don't "nicely" close a socket, the other side won't notice and keeps waiting for data. 如果您不“很好地”关闭套接字,那么另一端将不会注意到并继续等待数据。 That's why you have to really think about what you do with exceptions here and when to block and for how long. 这就是为什么您必须真正考虑在这里如何处理异常,何时阻止以及持续多长时间。 A lost connection can only be discovered when it throws an IOException on next attempt to read or write. 只有在下次尝试读取或写入时抛出IOException时,才能发现丢失的连接。 "next" in the sense of attempt started after connection has been lost. 在失去连接之后尝试开始的意义上的“下一个”。

This answer is not nearly covering everything, but I hope this will give you some hints. 这个答案几乎不能涵盖所有内容,但我希望这会给您一些提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM