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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:25:27+00:00 2026-05-19T04:25:27+00:00

I rewrote some of the code to implement the two answers below, and low

  • 0

I rewrote some of the code to implement the two answers below, and low and behold I got it to send 1 image, but now I just need it to keep sending, but the server finishes after 1 single image.

Client (Revised):

public void transferImage() {

File file = new File(ServerStats.clientFolder);
String[] files = file.list();
int numFiles = files.length;
boolean done = false;
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
byte[] byteArray;
long count;
long len;

Socket socket = null ;
while (!done){

  try {
    socket = new Socket(ServerStats.imgServerName,ServerStats.imgServerPort) ;
    InputStream inStream = socket.getInputStream() ;
    OutputStream outStream = socket.getOutputStream() ;
    System.out.println("Connected to : " + ServerStats.imgServerName);

    BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
    PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);

    for (int itor = 0; itor < numFiles; itor++) {

      String fileName = files[itor];
      System.out.println("transfer: " + fileName);
      File sentFile = new File(fileName);
      len = sentFile.length();
      System.out.println(len);
      out.println(len);
      out.println(sentFile);


      //SENDFILE
      bis = new BufferedInputStream(new FileInputStream(fileName));
      bos = new BufferedOutputStream(socket.getOutputStream( ));
      byteArray = new byte[1000000];
      count = 0;
      while ( (num = bis.read(byteArray)) >0 ){
        //num = bis.read(byteArray);
        bos.write(byteArray,0,num);
      }
      bos.close();
      bis.close();
      System.out.println("file done: " + itor);
    }
    done = true;
  } catch (IOException ee){
    System.err.println("FileIO Error: " + ee);
  }
}

}

Server (Revised):

public static void main(String[] args) {
BufferedInputStream bis;
BufferedOutputStream bos;
int num;
File file = new File(ServerStats.serverFolder);
if (!(file.exists())){
  file.mkdir();
}

try {
  ServerSocket socket = new ServerSocket(ServerStats.imgServerPort);
  Socket incoming = socket.accept();

  try {
    try{
      if (!(file.exists())){
        file.mkdir();
      }
      InputStream inStream = incoming.getInputStream();
      OutputStream outStream = incoming.getOutputStream();

      BufferedReader inm = new BufferedReader(new InputStreamReader(inStream));
      PrintWriter out = new PrintWriter(outStream, true /* autoFlush */);
      String length2 = inm.readLine();
      System.out.println(length2);
      String filename = inm.readLine();
      System.out.println("Filename = " + filename);
      out.println("ACK: Filename received = " + filename);

      //RECIEVE and WRITE FILE
      byte[] receivedData = new byte[1000000];
      bis = new BufferedInputStream(incoming.getInputStream());
      bos = new BufferedOutputStream(new FileOutputStream(ServerStats.serverFolder + "/" + filename));

      //long length = (long)Integer.parseInt(length2);
      //length++;
      while ( (num = bis.read(receivedData)) > 0){
        bos.write(receivedData,0,num);
      }
      //System.out.println(counter);
      bos.close();
      bis.close();

      File receivedFile = new File(filename);
      long receivedLen = receivedFile.length();
      out.println("ACK: Length of received file = " + receivedLen);

    } finally {
      incoming.close();
   }
  } catch (IOException e){
    e.printStackTrace();
  }
} catch (IOException e1){
  e1.printStackTrace();
}

}

Output:

    Welcome to DrJava.  Working directory is C:\Documents and Settings\Dapper Dan\Desktop
> run ServerQueue.Client
Connected to : localhost
transfer: Sunset.jpg
71189
file done: 0
transfer: Water lilies.jpg
83794
FileIO Error: java.net.SocketException: Socket is closed
Connected to : localhost
transfer: Sunset.jpg
71189
> 

That is the output on the client side, and the only image that got transfered was Sunset.jpg as indicated by file done statement.

Any ideas on what is causing this?

  • 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-19T04:25:27+00:00Added an answer on May 19, 2026 at 4:25 am

    On the client side, move the socket (and associated stream) creation under the numFiles iterator loop

    for (int itor = 0; itor < numFiles; itor++) {
    //socket = new Socket(...
    //socket's outputstream = ....
    

    On the server side, move socket.accept call and all logic below it under a while(true) loop.

    • 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.