【问题标题】:EOF Exception in java while sending multiple files through single socket connection [duplicate]通过单个套接字连接发送多个文件时java中的EOF异常[重复]
【发布时间】:2019-06-26 08:18:27
【问题描述】:

我正在尝试通过 java 套接字发送多个文件。成功接收到第一个文件后,它会抛出 EOFexception。我无法弄清楚出了什么问题。 (发送方发送所有文件成功)

发件人代码:

    sendToServer = new Socket(receiver,port);
    DataOutputStream out = new DataOutputStream(sendToServer.getOutputStream());

    for(File f: file_to_send){

        sendMessage = f.getName() + "\n"+ f.length() + "\n";
        out.writeUTF(sendMessage);

        FileInputStream requestedfile = new FileInputStream(f.getPath());
        System.out.println("file path: "+f.getPath());

        int count;
        byte[] buffer = new byte[8192];
        while ((count = requestedfile.read(buffer)) > 0)
        {
            out.write(buffer, 0, count);
        }

        System.out.println("File transfer completed!! voila! :)");
        requestedfile.close();
    }

    out.close();
    sendToServer.close();

收货人代码:

System.out.println("File Count : " + fileCount);
for (int count =0; count<fileCount; count++){
            String fileName = dis.readUTF();
            int length = Integer.parseInt(fileName.split("\n")[1]);
            fileName = fileName.split("\n")[0];
            System.out.println("File Name : " + fileName);
            System.out.println("Length : " + length);
            System.out.println("File Data : ");
            FileOutputStream fos = new FileOutputStream(new File(fileName));
            int c;
            byte[] buffer = new byte[8192];
            while ((c = dis.read(buffer)) > 0)
            {
                fos.write(buffer, 0, c);
                fos.flush();
            }
            fos.close();
            System.out.println("\nFile received successfully!!! voila !! :)");               
        }

输出是这样的:

Files Count : 2
File Name : Belly Dance.3gp
Length : 15969978
File Data : 

File received successfully!!! voila !! :)
java.io.EOFException
at java.base/java.io.DataInputStream.readUnsignedShort(DataInputStream.java:345)
at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:594)
at java.base/java.io.DataInputStream.readUTF(DataInputStream.java:569)
at App.java.DiscoveryClient.HandleInputMessage(DiscoveryClient.java:130)
at App.java.DiscoveryClient.run(DiscoveryClient.java:44)
at java.base/java.lang.Thread.run(Thread.java:834)

【问题讨论】:

    标签: java sockets networking serversocket eofexception


    【解决方案1】:

    接收文件的代码在读取第一个文件后并没有停止,而是继续读取直到流结束,将发送的所有内容写入同一个文件。

    您需要跟踪已读取的字节数和/或仍需读取的字节数:

            int remainingBytes = length;
            while (remainingBytes > 0 && (c = dis.read(buffer, 0, Math.min(buffer.length, remainingBytes))) > 0)
            {
                remainingBytes -= c;
                fos.write(buffer, 0, c);
                // fos.flush(); this is not really necessary
            }
    

    顺便说一句,使用int 作为文件长度会限制您使用最多 2GB 的文件。如果这是一个问题,请使用long 而不是int。

    【讨论】:

    • 永远不要在循环内冲洗。它消除了可能存在的任何输出缓冲的好处。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 2015-10-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多