【问题标题】:DataInputStream read() always return -1DataInputStream read() 总是返回 -1
【发布时间】:2020-04-29 09:02:37
【问题描述】:

我在java中有一个客户端-服务器程序,客户端向服务器发送请求,服务器通过发送文件来响应。 与服务器的连接正确,客户端发送的第一个请求被服务器正确解释。

此时,服务器读取一个特定文件,该文件似乎读得很好,因为写入发送了正​​确的字节数和字符串(通过打印字节数和字符串的打印进行测试)。

发送第一条消息后,客户端等待接收文件,但一旦收到消息,读取返回-1,好像它已经读取了一个EOF,我无法理解原因,如何解决它?

发送文件时服务器上的方法

private void sendFile(Socket client, String path, DataOutputStream writer) {
        File file = new File(path);
        byte[] bytes = new byte[1024];
        InputStream in = null;
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int count;
        try {
            while ((count = in.read(bytes)) > 0) {
                System.out.println("nBytes " + count);
                String tmp = new String(bytes);
                System.out.println(tmp);

                //i'm reading from a json file this string
                //[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
                //and it is printed correctly

                writer.write(bytes, 0, count);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

客户端方法

public void getFile (String username) {
        // first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message

        Socket socket = connect2server();
        DataInputStream reader = null;
        BufferedWriter writer = null;


        try {
            reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //send message to the server using writer 

        OutputStream out = null;
        String file = username+".json";
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        byte[] bytes = new byte[1024];

        int count;
        try {
            while ((count = reader.read(bytes)) >= 0) {
                System.out.println("nByte " + count);
                out.write(bytes, 0, count);
            }

            // the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message

            System.out.println(count);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            out.close();
            reader.close();
            writer.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

【问题讨论】:

    标签: java file server client datainputstream


    【解决方案1】:

    关于write,您不会关闭/刷新输出流,这反过来不会释放与流相关的任何资源。你有两个选择:

    1. 关闭调用其flush方法的输出流,然后 然后调用其底层输出流的 close 方法。
    2. 刷新输出流缓冲区。缓冲主要是为了提高I/O 的性能。

    来自doc,总合约read()

    从输入流中读取数据的下一个字节。值字节作为 int 返回,范围为 0 到 255。如果由于到达流的末尾而没有可用的字节,则返回值 -1。此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常。 子类必须提供此方法的实现。

    返回: 数据的下一个字节,如果到达流的末尾,则为 -1。

    【讨论】:

      【解决方案2】:

      在您的sendFile() 中,作者未关闭。您需要flush() 然后close() writer 以保证创建输出。也许这会解决你的问题

      【讨论】:

        猜你喜欢
        • 2014-07-27
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        • 2019-10-22
        • 2016-11-21
        • 2019-10-10
        • 2014-04-13
        • 2017-03-20
        相关资源
        最近更新 更多