File over socket problem

J

jwarzech

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");
 
K

Knute Johnson

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?
 
D

Doezel

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

Knute Johnson

Doezel said:
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.
 
E

Esmond Pitt

Knute said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top