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

  • Home
  • SEARCH
  • 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 8639943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:09:08+00:00 2026-06-12T11:09:08+00:00

I have a java server that sends files to an android client. for some

  • 0

I have a java server that sends files to an android client. for some reason, the server seems to be closing the socket after only one pass through, I want it to send all files then close socket, here is my server code. Thanks in advance

public void run() {
   DataOutputStream dos = null;
    try {
        String path = "/home/test";
        int count = 0;
        dos = new DataOutputStream(
                new BufferedOutputStream(socket.getOutputStream()));
        DataInputStream dis = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));

        File[] files = new File(path).listFiles();
        for (File file : files) {
            String filename = file.getName();
            String extension = filename.substring(
                    filename.lastIndexOf(".") + 1, filename.length());
            if ("png".equals(extension)) {
                count += 1;
            }

        }
        System.out.println("Sending " + count + " files");
        dos.writeInt(count);
        dos.flush();
        byte[] temp = new byte[1024];
        int n = 0;
        for (File file : files) {
            String filename = file.getName();
            String extension = filename.substring(
                    filename.lastIndexOf(".") + 1, filename.length());
            if ("png".equals(extension)) {
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                byte fileContent[] = new byte[(int) file.length()];
                bis.read(fileContent);
                int dataLength = fileContent.length;
                dos.writeInt(dataLength);
                dos.flush();
                int open = dis.readInt();
                System.out.println("still open if open = 5: open = "+open);
                System.out.println(filename + " is " + dataLength
                        + " bytes long");
                while ((dataLength > 0)
                        && (n = bis.read(temp, 0,
                                (int) Math.min(temp.length, dataLength))) != -1) {
                    dos.write(temp, 0, n);
                    dos.flush();
                    dataLength -= n;

                }
                System.out.println("Sent file "+filename);
                fis.close();
                bis.close();

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    finally
    {   

        try{
        if (dos != null) {dos.close();}
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

}

  • 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-12T11:09:10+00:00Added an answer on June 12, 2026 at 11:09 am

    Any chance your Android application is spinning? You have a logic error in your code. It reads in the whole file into fileContent but then you go into your while loop waiting for the file to be fully read.

    bis.read(fileContent);
    ...
    while ((dataLength > 0)
        && (n = bis.read(temp, 0, (int) Math.min(temp.length, dataLength))) != -1) {
    

    You’ve already read in the entire file from bis so when you are reading more you are getting n == 0 so it is looping forever.

    If you want to do the buffering, then you should remove the bis.read(fileContent);. If you want to read the file at the start then you need to remove the while loop.

    int dataLength = (int) file.length();
    byte fileContent[] = new byte[dataLength];
    bis.read(fileContent);
    dos.writeInt(dataLength);
    dos.write(fileContent);
    dos.flush();
    dataLength -= n;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Java server using Eclipse that accepts TCP socket connection from
i have a java chat server & client, that works fine. I made a
I have a client on Android and server on c#. The client sends messages
I have created a basic Client Server that will send image files in a
I have a Java client (client server app) that does two main things: (1)
I have a desktop client that sends data to a web server and I
Java Socket Server I have a Java process that is creating a listener on
I have coded a server in Java that will have several clients connected to
I have a java JAR file that is triggered by a SQL server job.
I have created a java server, which takes screenshots, resizes them, and sends them

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.