Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8632087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:18:16+00:00 2026-06-12T09:18:16+00:00

I am implementing two programs; Client and Server, and client asks for a file

  • 0

I am implementing two programs; Client and Server, and client asks for a file from server to download in local file system.

After downloading one file, the client should be able to download another file if it wishes to..

However after it downloads the file, Server gives me an exception says

java.net.SocketException: Socket closed

Here’s my code..

Client:

        byte[] aByte = new byte[0];
        int bytesRead;
        String msg;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        while (!(msg = input.readLine()).equals("end")) {

            String myFolderName = "ServerFolder";
            File folder=new File(myFolderName);   
            if (!folder.exists()){ 
                folder.mkdir();
            }
            System.out.println("file downloading");
            try {

                System.out.println("1");
                fos = new FileOutputStream("ServerFolder/"+fileToDownload);
                bos = new BufferedOutputStream(fos);

                System.out.println("MSG: "+msg);

                bytesRead = in.read(aByte, 0, aByte.length);

                System.out.println("2");



                do {
                    baos.write(aByte);
                    bytesRead = in.read(aByte);
                } while (bytesRead != -1);

                System.out.println("3");
                bos.write(baos.toByteArray());
                bos.flush();
            } catch (IOException ex) {
                System.err.println(ex.getMessage());
            }


        }
        bos.close();

    } catch (IOException ex) {
        System.err.println(ex);
    }    

Server:

if (outToClient != null) {
                    System.out.println("2");
                    File myFile = new File(msg);
                    byte[] mybytearray = new byte[(int) myFile.length()];


                    try {
                        fis = new FileInputStream(myFile);
                    } catch (FileNotFoundException ex) {
                        // Do exception handling
                    }
                    System.out.println("3");
                    bis = new BufferedInputStream(fis);

                    try {
                        bis.read(mybytearray, 0, mybytearray.length);   

                        System.out.println("mybytearray.length: "+(int) myFile.length());
                        out.write((int) myFile.length()+"\r\n");    
                        out.write("end\r\n");
                        out.flush();


                        outToClient.write(mybytearray, 0, mybytearray.length);
                        outToClient.flush();

                        s.shutdownOutput();

                        outToClient.close();


                        System.out.println("4");

                    } catch (IOException ex) {
                        System.out.println("5");
                        System.err.println(ex);
                    }

(All connection stuff are done in the beginning of each method)

I had

s.close();

in the Server but I deleted it just in case it causes the error but its not..

I am presuming that

outToClient.close();

is not causing it either?…

Also I googled this problem and some people suggested to tell the client the size of the file before the server sends the file.. but it didn’t work as well.. so I deleted that part as well(or maybe I did it wrong..)

Thanks:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:18:17+00:00Added an answer on June 12, 2026 at 9:18 am

    java.net.SocketException: Socket closed

    This has one meaning only. You closed the socket, then you continued you use it. Possibly you are unware that closing either the input or the output stream of a Socket closes the other stream and the socket.

    There are numerous other errors in your code: you are ignoring the value returned by read(); you are using unnecessary BytearrayOutputStreams when you could be writing directly to the target; etc etc. Too numerous to mention really. The canonical way to copy a stream in Java is as follows:

    while ((count = in.read(buffer)) > 0)
    {
      out.write(buffer, 0, count);
    }
    

    If you want to use the same connection to transfer more than one file you will have to send the length of the file ahead of the file so the peer knows when it has read all the file, using an obvious modification of the above loop. DataOutputStream.writeLong() and DataInputStream.readLong() provide the most obvious ways of doing that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to Write two Java programs implementing the FTP server with responses
I'm implementing a login/authentication system for my little server-client program. I'm wondering how to
I am implementing two processes on a LynxOS SE (POSIX conformant) system that will
I have two Java interfaces and one implementing class. (I have used Eclipse to
Imagine two simple java applications. Both of them are implementing the same JAR file
In c++11 how would one go about implementing a program that does two expensive
Possible Duplicate: StructureMap singleton usage (A class implementing two interface) I'm currently designing a
I have an interface and two objects implementing that interface, massively simplied; public interface
I am implementing many SSB working on two different instances. They are data push
So I'm implementing the repository pattern in an application and came across two issues

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.