简体   繁体   中英

Socket file transfer failing

I have a server which uses the Sender class and a client which uses the Downloader class below. When the client connects to the server, it downloads the database.db and the default.docx perfectly, but when It starts to read the .png files, It throws this error:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.read(BufferedInputStream.java:308)
at java.io.DataInputStream.readFully(DataInputStream.java:178)
at java.io.DataInputStream.readLong(DataInputStream.java:399)
at data.Downloader.<init>(Downloader.java:18)
at data.Connection.<init>(Connection.java:18)
at main(Client.java:17)

Here are my only two methods:

Downloader:

public Downloader(Socket socket) {
    try {
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        DataInputStream dis = new DataInputStream(bis);
        int filesCount = dis.readInt();
        for (int i = 0; i < filesCount; i++) {
            long size = dis.readLong();
            String fileName = dis.readUTF();
            System.out.println(fileName);
            if (fileName.equals("database.db")) {
                List<String> data = new ArrayList<String>();
                BufferedReader reader = new BufferedReader(new InputStreamReader(bis));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.trim().length() > 0) {
                        data.add(line);
                    }
                }
                reader.close();
                parse(data);
            } else if (fileName.equals("default.docx")) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                for (int x = 0; x < size; x++) {
                    bos.write(bis.read());
                }
                bos.close();
                document = bos.toByteArray();
            } else if (fileName.contains(".png")) {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                for (int x = 0; x < size; x++) {
                    bos.write(bis.read());
                }
                bos.close();
                Signatures.signatures.add(bos.toByteArray());
            } 
        }
        dis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Sender:

public Sender(Socket socket) {
    List<File> files = new ArrayList<File>();
    files.add(new File(Directory.getDataPath("default.docx")));
    files.add(new File(Directory.getDataPath("database.db")));
    for (String signature : Directory.getSignaturePaths()) {
        files.add(new File(signature));
    }
    try {
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeInt(files.size());
        for (File file : files) {
            System.out.println(file.getName() + file.length());
            dos.writeLong(file.length());
            dos.writeUTF(file.getName());
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            int theByte = 0;
            while ((theByte = bis.read()) != -1) { 
                bos.write(theByte);
            }
            bis.close();
        }
        dos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Also, I checked the getSignaturePaths() method and It returns the correct paths and the .png files are there.

You closed the stream and then kept using the socket. Closing either the input stream or the output stream of a socket closes the other stream and the socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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