首页 > 解决方案 > 如何修复线程“main”java.io.EOFException中的异常?(Java 套接字)

问题描述

我想使用 java 套接字创建一个简单的多文件传输程序。

服务器输出:

I am server
The server is listening...
Client are connected
Files Selected : 2
README.txt
vcruntime140.dll
Server Closed!

客户端输出:

i am client
Server are connected
filecount : 2
filenames : README.txt

在 socket.client.main(client.java:32) 的 java.io.DataInputStream.readLong(Unknown Source) 的 java.io.DataInputStream.readFully(Unknown Source) 的线程“main”java.io.EOFException 中的异常

这是我的服务器代码!服务器是发件人。

public static void main(String[] args) throws Exception {

    System.out.println("i am server");
    System.out.println("");
    server = new ServerSocket(12345);
    System.out.println("Server is listening...");
    client = server.accept();
    System.out.println("Client are connected");
    dos = new DataOutputStream(client.getOutputStream());


    dir = "C:\\Users\\Nitesh Rathi\\Downloads\\vcruntime140";
    files = new File(dir).listFiles();
    System.out.println("Files Selected : " + files.length);

    dos.writeInt(files.length);

    byte[] b = new byte[4096];

    for (File file : files)
    {
        long length = file.length();
        dos.writeLong(length);

        String filename = file.getName();
        dos.writeUTF(filename);

        System.out.println(file.getName());
        fis = new FileInputStream(file);

        while (fis.read(b) != -1)
        {
            fis.read(b, 0, b.length);
            dos.write(b, 0, b.length);
        }
    }

    System.out.println("");
    fis.close();
    client.close();
    server.close();
    System.out.println("Server Closed!");
}

这是我的客户代码!客户是接收者。

public static void main(String[] args) throws Exception {

    System.out.println("i am client");
    System.out.println("");
    soc = new Socket("localhost", 12345);
    System.out.println("Server are connected");
    dis = new DataInputStream(soc.getInputStream());

    int filecount = dis.readInt();
    File[] files = new File[filecount];

    System.out.println("filecount : " + filecount);
    byte[] b = new byte[1024];

    for (int i=0;i<filecount;i++)
    {
        long filelength = dis.readLong();
        String filename = dis.readUTF();
        System.out.println("filenames : "+filename);

        files[i] = new File(dirPath + "/" + filename);
        fos = new FileOutputStream(files[i]);

        for(int j = 0; j < filelength; j++)
        {
            fos.write(b);
            dis.read(b);
        }

    }

    System.out.println("data received!");

    fos.flush();
    fos.close();
    soc.close();

    System.out.println("client closed!");
}

我希望客户端的这个输出:文件计数:2 文件名:README.txt vcruntime140.dll

标签: javasocketsfile-handling

解决方案


在服务器端,您在fis.read()从输入文件流读取时每次循环迭代调用两次。每次迭代只需要调用一次,并且需要传递 to 的返回值,fis.read()以便dos.write()它知道要写入的正确字节数。

在客户端,在读取文件流时,您是在调用填充数据fos.write()之前调用的。而且您循环了太多次,因为您没有使用每次迭代实际读取的字节数来更新循环计数器。dis.read()bj

尝试更多类似的东西:

服务器:

public static void main(String[] args) throws Exception
{
    System.out.println("i am server");
    System.out.println("");

    ServerSocket server = new ServerSocket(12345);
    System.out.println("Server is listening...");

    Socket client = server.accept();
    System.out.println("Client is connected");

    DataOutputStream dos = new DataOutputStream(client.getOutputStream());

    String dir = "C:\\Users\\Nitesh Rathi\\Downloads\\vcruntime140";
    Files[] files = new File(dir).listFiles();

    System.out.println("Files Selected : " + files.length);
    dos.writeInt(files.length);

    byte[] b = new byte[4096];

    for (File file : files)
    {
        long filelength = file.length();
        dos.writeLong(filelength);

        String filename = file.getName();
        dos.writeUTF(filename);
        System.out.println(filename);

        FileInputStream fis = new FileInputStream(file);
        DataInputStream dis = DataInputStream(fis);

        int loops = (int) (filelength / (long) b.length);
        int remainder = (int) (filelength % (long) b.length);

        for (int j = 0; j < loops; j++) 
        {
            dis.readFully(b);
            dos.write(b);
        }

        if (remainder > 0)
        {
            dis.readFully(b, 0, remainder);
            dos.write(b, 0, remainder);
        }
    }

    System.out.println("");

    dos.close();
    server.close();

    System.out.println("Server Closed!");
}

客户:

public static void main(String[] args) throws Exception
{
    System.out.println("i am client");
    System.out.println("");

    Socket soc = new Socket("localhost", 12345);
    System.out.println("Server is connected");

    DataInputStream dis = new DataInputStream(soc.getInputStream());

    int filecount = dis.readInt();
    File[] files = new File[filecount];
    System.out.println("filecount : " + filecount);

    byte[] b = new byte[1024];

    for (int i = 0; i < filecount; i++)
    {
        long filelength = dis.readLong();
        String filename = dis.readUTF();
        System.out.println("filename : " + filename);

        files[i] = new File(dirPath + "/" + filename);

        FileOutputStream fos = new FileOutputStream(files[i]);

        int loops = (int) (filelength / (long) b.length);
        int remainder = (int) (filelength % (long) b.length);

        for (int j = 0; j < loops; j++)
        {
            dis.readFully(b);
            fos.write(b);
        }

        if (remainder > 0)
        {
            dis.readFully(b, 0, remainder);
            fos.write(b, 0, remainder);
        }
    }

    System.out.println("data received!");

    dis.close();

    System.out.println("client closed!");
}

推荐阅读