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");
Knute Johnson - 26 Apr 2007 05:27 GMT
> 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
[quoted text clipped - 31 lines]
> //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?

Signature
Knute Johnson
email s/nospam/knute/
Doezel - 26 Apr 2007 08:17 GMT
>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
[quoted text clipped - 31 lines]
> //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 - 26 Apr 2007 08:20 GMT
> while((r = input.read(buffer)) > 0)
> {
> out.write(buffer,0,r);
> }
You should check if r>-1 instead of r>0.
Knute Johnson - 26 Apr 2007 16:56 GMT
>> 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.

Signature
Knute Johnson
email s/nospam/knute/
Esmond Pitt - 30 Apr 2007 06:01 GMT
>>> while((r = input.read(buffer)) > 0)
>>> {
[quoted text clipped - 6 lines]
> 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.
Z. - 26 Apr 2007 17:56 GMT
>> 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).