File over socket problem

Discussion in 'Java' started by jwarzech@gmail.com, Apr 25, 2007.

  1. Guest

    I am trying to send a zip file from a client to a server using
    sockets. I can get the file to transfer all right but the server
    "halts" after the send. Using trace debugging I can see that the while
    loop is exiting but any statement afterwards is not being processed.

    Client Code

    output = new DataOutputStream(client.getOutputStream());
    byte[] buffer = new byte[1024];
    int r;
    InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
    while((r = in.read(buffer)) > 0)
    {
    output.write(buffer,0,r);
    }
    output.flush();
    input.close();
    output.close();



    Server Code

    input = new DataInputStream(connection.getInputStream());
    OutputStream out = new FileOutputStream(outFile);
    byte[] buffer = new byte[1024];
    int r;
    while((r = input.read(buffer)) > 0)
    {
    out.write(buffer,0,r);
    }
    out.flush();
    out.close();
    input.close();

    //This doesn't display, any idea??
    System.out.println("Completed");
    , Apr 25, 2007
    #1
    1. Advertising

  2. wrote:
    > I am trying to send a zip file from a client to a server using
    > sockets. I can get the file to transfer all right but the server
    > "halts" after the send. Using trace debugging I can see that the while
    > loop is exiting but any statement afterwards is not being processed.
    >
    > Client Code
    >
    > output = new DataOutputStream(client.getOutputStream());
    > byte[] buffer = new byte[1024];
    > int r;
    > InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
    > while((r = in.read(buffer)) > 0)
    > {
    > output.write(buffer,0,r);
    > }
    > output.flush();
    > input.close();
    > output.close();
    >
    >
    >
    > Server Code
    >
    > input = new DataInputStream(connection.getInputStream());
    > OutputStream out = new FileOutputStream(outFile);
    > byte[] buffer = new byte[1024];
    > int r;
    > while((r = input.read(buffer)) > 0)
    > {
    > out.write(buffer,0,r);
    > }
    > out.flush();
    > out.close();
    > input.close();
    >
    > //This doesn't display, any idea??
    > System.out.println("Completed");
    >


    Without seeing all of the code it is going to be difficult to diagnose.
    Are you checking for Exceptions?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 26, 2007
    #2
    1. Advertising

  3. Doezel Guest

    >I am trying to send a zip file from a client to a server using
    > sockets. I can get the file to transfer all right but the server
    > "halts" after the send. Using trace debugging I can see that the while
    > loop is exiting but any statement afterwards is not being processed.
    >
    > Client Code
    >
    > output = new DataOutputStream(client.getOutputStream());
    > byte[] buffer = new byte[1024];
    > int r;
    > InputStream in = new FileInputStream(path + "\\" + file+ ".zip");
    > while((r = in.read(buffer)) > 0)
    > {
    > output.write(buffer,0,r);
    > }
    > output.flush();
    > input.close();
    > output.close();
    >
    >
    >
    > Server Code
    >
    > input = new DataInputStream(connection.getInputStream());
    > OutputStream out = new FileOutputStream(outFile);
    > byte[] buffer = new byte[1024];
    > int r;
    > while((r = input.read(buffer)) > 0)
    > {
    > out.write(buffer,0,r);
    > }
    > out.flush();
    > out.close();
    > input.close();
    >
    > //This doesn't display, any idea??
    > System.out.println("Completed");


    I'm not that experienced with java. But I think the server keeps on waiting
    for input. How would the server know when the client doesn't want to send
    anymore? I would at first send the filesize to the server and let the server
    read that number of bytes. That way the server can close itself after it has
    received enough bytes.
    Doezel, Apr 26, 2007
    #3
  4. Doezel Guest

    > while((r = input.read(buffer)) > 0)
    > {
    > out.write(buffer,0,r);
    > }


    You should check if r>-1 instead of r>0.
    Doezel, Apr 26, 2007
    #4
  5. Doezel wrote:
    >> while((r = input.read(buffer)) > 0)
    >> {
    >> out.write(buffer,0,r);
    >> }

    >
    > You should check if r>-1 instead of r>0.


    Doezel has a point there. That could be what is hanging your program.
    The read returns 0 bytes sometimes before it returns -1 to signal end of
    stream. Anyway 0 bytes is possible.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Apr 26, 2007
    #5
  6. Z. Guest

    Doezel wrote:
    >> while((r = input.read(buffer)) > 0)
    >> {
    >> out.write(buffer,0,r);
    >> }


    > You should check if r>-1 instead of r>0.


    NO! Check for != -1.

    It's possible to read 0 bytes before "End Of Stream" (-1).
    Z., Apr 26, 2007
    #6
  7. Esmond Pitt Guest

    Knute Johnson wrote:
    > Doezel wrote:
    >
    >>> while((r = input.read(buffer)) > 0)
    >>> {
    >>> out.write(buffer,0,r);
    >>> }

    >>
    >>
    >> You should check if r>-1 instead of r>0.

    >
    >
    > Doezel has a point there. That could be what is hanging your program.
    > The read returns 0 bytes sometimes before it returns -1 to signal end of
    > stream. Anyway 0 bytes is possible.


    No he doesn't. InputStream.read() is specified to return either -1
    meaning end of stream or a value between 1 and the implicit or explicit
    buffer size. Reading zero bytes is only possible if you supply a length
    of zero, or in channel I/O in non-blocking mode.
    Esmond Pitt, Apr 30, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,806
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    960
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    542
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    655
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    713
Loading...

Share This Page