Java套接字客户端没有接收数据

时间:2014-08-26 07:41:41

标签: java sockets

我似乎在使用Java中的套接字时遇到了一些问题。问题是客户端没有接收数据,并且当它有时收到数据时,数据会延迟很长时间。服务器正确地从客户端接收所有数据并响应我希望的方式(至少从它的输出中做出响应)。任何有关这方面的帮助都会有所帮助。

服务器:

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

客户端:

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

服务器控制台输出:

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

客户输出:

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

此外,即使服务器识别出客户端正在尝试发送数据,服务器有时也不会从客户端接收数据。

1 个答案:

答案 0 :(得分:0)

如果你不熟悉&#34;关闭套接字,另一方没有注意到并且一直在等待数据。 这就是为什么你必须真正考虑你在这里做什么以及何时阻止和持续多久。只有在下一次尝试读取或写入时抛出IOException时,才能发现丢失的连接。 &#34;下一个&#34;在连接丢失后尝试开始。

这个答案并不能涵盖所有内容,但我希望这会给你一些提示。