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 8618791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:11:30+00:00 2026-06-12T06:11:30+00:00

I have an android client and a multithreaded Java server. The server was originally

  • 0

I have an android client and a multithreaded Java server. The server was originally written in Python and worked great, but now that I re-wrote it in Java it doesn’t seem to be working. Below is my server code. It may be worth noting that the Python implementation was not multithreaded, but I don’t think I would need to change the client for that anyway.

import java.net.*;
import java.io.*;
import org.apache.commons.io.FileUtils;

public class MultiServerThread extends Thread {
    private Socket socket = null;

    public MultiServerThread(Socket socket) {
        super("MultiServerThread");
        this.socket = socket;
    }

    @Override
    public void run() {

        try {
            String path = "C:/Users/LandClan/Desktop/cubikal";
            int count = 0;
            DataOutputStream 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);
            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);
                    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();
                }
            }
            for (File file1 : files) {
                String filename = file1.getName();
                String extension = filename.substring(
                        filename.lastIndexOf(".") + 1, filename.length());
                if ("txt".equals(extension)) {
                    FileInputStream fis = new FileInputStream(file1);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    byte fileContent[] = new byte[(int) file1.length()];
                    bis.read(fileContent);
                    int dataLength = fileContent.length;
                    dos.writeInt(dataLength);
                    System.out.println("file is " + dataLength + "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");
                    fis.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

here is the first part of the server

package server;

import java.net.*;
import java.io.*;

public class Server {
    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try {
            serverSocket = new ServerSocket(4447);
        } catch (IOException e) {
            System.err.println("Could not liten on port: 4447.");
            System.exit(-1);
        }

        while (listening) {
            new MultiServerThread(serverSocket.accept()).start();
        }
        serverSocket.close();
    }
}
  • 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-12T06:11:31+00:00Added an answer on June 12, 2026 at 6:11 am

    The DataOutputStream never seems to get closed, and the DataInputStream is never used at all.

    Get rid of the DataInputStream , and make sure you close the DataOutputStream when you have finished with it.

    A good way is to add a finally{} block to your try-catch, although you’ll need to declare the DataOutputStream outside of the try-catch so it is visible in the finally.

    DataOutputStream dos = null;
    try
    {
        DataOutputStream dos = new DataOutputStream(
                    new BufferedOutputStream(socket.getOutputStream()));
        // do stuff
    }
    catch(IOException e)
    {
        //stacktrace etc
    }
    finally
    {
        if (dos != null) dos.close();
    }
    

    This kind of stuff is always a bit ugly in Java, though upcoming versions may make it better…

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

Sidebar

Related Questions

I have a java server that sends files to an android client. for some
I have Android client app that communicates with server using Socket . On my
I have a client and a server app on Android (that uses ZeroC -
I have an Android client that needs to authenticate with a python Google App
i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;
I have a Java server and an Android Client. The server sends the dimension
I have a wamp server. I have written my android client. If I run
I have two Java applications, where an Android client connects to a server on
I have and android client, that uses ksoap to communicate with a wsdl web
I have dynamic listview on my android client app that receive data from remote

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.