Java Socket auto close itself after File transfer Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Java Socket auto close itself after File transfer without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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);

  , 0, mybytearray.length);
            os = connsock.getOutputStream();
            System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + "bytes)");
            os.write(mybytearray, 0, mybytearray.length);
        } finally {
            if (bis != null) {
            if (os != null) {

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 =, 0, mybytearray.length);
            // current = bytesRead;

            while ((bytesRead = > 0) {
                bos.write(mybytearray, 0, bytesRead);
            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
        } finally {
            if (fos != null) {
            if (bos != null) {

And this is what i get after the file transfer

SEVERE: null socket closed
    at Method)

What is wrong?


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 = > 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 =, 0, final_file_size);     
We are here to answer your question about Java Socket auto close itself after File transfer - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji