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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:49:03+00:00 2026-05-18T09:49:03+00:00

I have created a basic Client Server that will send image files in a

  • 0

I have created a basic Client Server that will send image files in a specified directory over a network. The code worked last week but I came back to it today and it seems that I am only getting one file on the server side, even though the client prints out that it has sent all the image files in the directory.
It may be something in the client code but I think it is something on the server side.
Any help is greatly appreciated and if you have a more efficient solution, I am happy to change my code as necessary. My code is below:

ImageServer

        package com.encima.network.server;

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

        public class ImageServer{

     ServerSocket ss;
     Socket s;
     ObjectOutputStream oos;
     int port = 4440;

     public ImageServer() throws IOException {
      try {
       ss  = new ServerSocket(port);
       System.out.println("Server started on Port: " + port);
      } catch(IOException e) {
       System.out.println("Serevr: Port-" + port  + " not available, exiting.");
       System.exit(0);
      }

      System.out.println("Server: Waiting for Client Connection...");

      while(true) {
       try {
        s = ss.accept();
        new ImageHandler(s);
       } catch (IOException e) {
        e.printStackTrace();
       }
      }
     }

     public static void main(String[] args) throws IOException {
      ImageServer is = new ImageServer();
     }

        }

ImageHandler

    package com.encima.network.server;

    import java.awt.image.BufferedImage;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    public class ImageHandler implements Runnable {

     Socket s;
     int count = 0;

     public ImageHandler(Socket socket) {
      s = socket;
      Thread t = new Thread(this);
      t.start();
     }

     @Override
     public void run() {

      try {
       ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
       FileOutputStream fos = new FileOutputStream("image" + System.nanoTime() + ".jpg");
       count++;
       //BufferedImage in = ImageIO.read(ois);
       //ImageIO.write(in, "jpg", fos);

       int ch = 0;
        while(true) {
         ch = ois.read();
          if(ch == -1) {
           break;
          }
         fos.write(ch);
        }   
       fos.flush();
      } catch (Exception e) {
       e.printStackTrace();
      }
     }
    }



Finally, the ImageClient

    package com.encima.network.client;

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.net.Socket;

    import javax.imageio.ImageIO;

    import com.encima.network.ImageFilter;


    public class ImageClient {

     Socket s;
     String ip = "localhost";
     int port = 4440;
     ObjectOutputStream oos;

     public ImageClient(File[] files) throws IOException, ClassNotFoundException, InterruptedException {

      try {
       s = new Socket(ip, port);
       System.out.println("Client connected to Server via " + ip + " on port 80");
      } catch (Exception e) {
       System.out.println("Client: Cannot find Host: " + ip + ". Exiting.");
       System.exit(0);
      }

      oos = new ObjectOutputStream(s.getOutputStream());

      for(File f: files) {
       sendFile(f);
      }
      oos.close();
       //System.out.println("Written Image " + i + " of " + files.length);
     }

     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
      File dir = new File("/Users/christophergwilliams/Dropbox/PhD/Projects/PhD/Year 1/GSN/images");
      File[] files = dir.listFiles(new ImageFilter());
      ImageClient ic = new ImageClient(files);
     }

     public void sendFile(File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      //BufferedImage b = ImageIO.read(file);
      //ImageIO.write(b, "jpg", oos);
      int ch = 0;
       while(true) {
        ch = fis.read();
        if(ch == -1) {
         break;
        }
        oos.write(ch);
       }
      oos.flush();
      System.out.println("Image Sent");

     }


}

I am aware that it is a lot of code to read through but I do appreciate any help I can get on this!

I may be wrong but, for the sake of efficiency and network traffic, would it be beneficial to send the images as a zip from the client to the server?

  • 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-05-18T09:49:03+00:00Added an answer on May 18, 2026 at 9:49 am

    Why are you using ObjectInputStream at all? You’re not reading or writing any serialized objects – just raw binary data. Use whatever InputStream is provided, and read from that.

    Anyway, that’s not the big problem. The big problem is that you’re just writing several files to one stream, with no indication of where one file is meant to finish and the next one is meant to start. How were you expecting to split the multiple files up? Options:

    • Use a delimiter between files (very ugly – you’d have to potentially escape any data which looked like the delimiter as you went along)
    • Prefix each file with its length
    • Send each file on a different connection

    (You’re also reading and writing a single byte at a time. Use the overloads of read/write which accept byte arrays.)

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

Sidebar

Related Questions

No related questions found

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.