Java Client-Server Problem When Sending Multiple Files

advertisements

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?


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.