views:

219

answers:

2

I am trying to send a file (an image sent as a byte array) with the client and then the server should receive said byte array to make further use of it. However when I click on the "send" to send the image the file transfer starts (as I get a sentImage.jpg in my Desktop) but it gets stuck for some reason I can't figure out and the image never gets correctly sent.

Here's the part that receives from the server (it already accepted the connection):

 public void run(){
   try {
          byte[] receivedData = new byte[1024];
          BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
         // while(bis.read() != -1){
          s.acquireUninterruptibly();
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Admin\\Desktop\\sentImage.jpg"));
          while ((incoming = bis.read(receivedData)) != -1) {
              bos.write(receivedData, 0, incoming);
          }
          s.release();
          n.release();
          bis.close();
          bos.flush();
        // }
      } catch (IOException e) {
          e.printStackTrace();
      }
}

and the client is sending here:

public void sendImageResult() {
      new Thread(new Runnable() {
       public void run() {
        try {
         int inside = 0;

         Socket socket = new Socket("localhost", 4444);

         File myImageFile = new File("C:\\Users\\Admin\\Desktop\\test.jpg");
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myImageFile));
         BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream( ));
         byte[] byteArray = new byte[1024];
         while ((inside = bis.read(byteArray)) != -1){
          bos.write(byteArray,0,inside);
         }
         bis.close();
         bos.flush();
        } catch (UnknownHostException ex) {
         System.out.println("No se pudo establecer la conexión.");
         ex.printStackTrace();
        } catch (FileNotFoundException fnf){
         fnf.printStackTrace();
        } catch(IOException ioe){
         ioe.printStackTrace();
        }

       }
      }).start();
     }
A: 

It does not appear that the OutputStream (bos) that is used to write to disk is being closed. This could lead to unexpected results.

jt
But not an obvious explanation for the problem.
bmargulies
True, but the flush itself doesn't release the resource. Relying on garbage collection to occur and force a close could be unpredictable. close() releases the resources: http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStream.html#close%28%29
jt
A: 

As jt said, the OutputStream writing to disk is not being closed, but neither is the OutputStream being used to send the data, nor is the Socket being closed from the sending side. The sending side may be buffering the data at the tcp level, waiting for more bytes before sending the last packet. You are calling flush, but that can be ignored, it's not guaranteed to work like you expect. Another thing to try is calling shutdownOutput on the Socket and seeing if that forces it to flush. You can also try setTcpNoDelay(true) when you open the Socket. If none of that works, get a tcp trace program (I like tcpdump) and use it to see if the packets are actually being sent, it will at least narrow it down to either the send or receive end of things.

joe p