Java Socket auto close itself after File transfer

Im trying to transfer a file using java and sockets. My problem is that after the successfull file transfer the socket closes itself at the server side(where i transfer the file) .

Here is my server where i try to send the file

public void send_file_to_client(String requested_file) throws IOException {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        OutputStream os = null;
        try {

            File FILE_TO_SEND = new File("C:\ServerMusicStorage\" + requested_file + ".wav");
            byte[] mybytearray = new byte[(int) FILE_TO_SEND.length()];

            fis = new FileInputStream(FILE_TO_SEND);
            bis = new BufferedInputStream(fis);

            bis.read(mybytearray, 0, mybytearray.length);
            os = connsock.getOutputStream();
            System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + "bytes)");
            toClient.writeUTF(Integer.toString(mybytearray.length));
            os.write(mybytearray, 0, mybytearray.length);
            os.flush();
            System.out.println("Done.");
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (os != null) {
                os.close();
            }
        }
    }

and here is the client where i receive the file

public static void receive_file(String requested_file) throws IOException {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;

        try {

            File file_to_save = new File("C:\ClientMusicStorage\" + requested_file + ".wav");
            int bytesRead;

            String fileSize = fromServer.readUTF();
            int final_file_size = Integer.parseInt(fileSize);

            byte[] mybytearray = new byte[final_file_size];
            InputStream is = newclientSocket.getInputStream();
            fos = new FileOutputStream(file_to_save);
            bos = new BufferedOutputStream(fos);
            // bytesRead = is.read(mybytearray, 0, mybytearray.length);
            // current = bytesRead;

            while ((bytesRead = is.read(mybytearray)) > 0) {
                bos.write(mybytearray, 0, bytesRead);
            }
            bos.flush();
            System.out.println("File " + requested_file + ".wav" + " downloaded (" + final_file_size + " bytes read)");
        } catch (EOFException e) {
            //eof - no error in this case
        } catch (IOException e) {
            //something went wrong
            e.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }

And this is what i get after the file transfer

SEVERE: null
java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.net.SocketInputStream.read(SocketInputStream.java:223)
    at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:337)
    at java.io.DataInputStream.readUTF(DataInputStream.java:589)
    at java.io.DataInputStream.readUTF(DataInputStream.java:564)

What is wrong?

Answer

It is basically because you are closing the output stream (Part of your finally block) as soon you are done with the transfer. But the client code is still trying to read data from the stream, refer to the below portion of the code

 while ((bytesRead = is.read(mybytearray)) > 0) 

so if you address this part you won’t run into this issue. Basically from the server you can send the size of the file to the client followed by the contents of the file . Then in client side you can just read the file size first and read the next bytes, You just have to replace while loop with below line.

int fileReadBytes = is.read(mybytearray, 0, final_file_size);     

Leave a Reply

Your email address will not be published. Required fields are marked *