File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Distributed Java and the fly likes Transferring a file across a socket connection--HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Transferring a file across a socket connection--HELP" Watch "Transferring a file across a socket connection--HELP" New topic

Transferring a file across a socket connection--HELP

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1130
I'm having a problem getting my entire file transferred across the socket connection. The first buffer comes across fine.
What's happening is that I do get the first set of bytes, but from then on, I keep getting the same bytes (on the client side) every time.
I think maybe the writeObject() I'm using to write a new array of bytes isn't working as I expected.
I print out on the server side what I've read and it's correctly advancing though the file. However, either the writeObject() call or something is incorrect. Basically, I'm trying to write the buffer I read on the server side to the client who reads it.
The server code does stuff like this:
// read a chunk into the buffer, then write them out.
// keep looping until we reach the end of the file
// (when read() returns -1).
bytes_read =;
for (int x = 0; x < buffer.length; x++)
outText.append((char) buffer[x]);
System.out.println("This read:\n" + outText);
// wait until client is ready for more.
// nothing...loop...
while (!input.readObject().equals("CLIENT>>> Read OK."));
} while (bytes_read != -1);
The client code looks like this:
buffer = (byte[]) input.readObject();
output.writeObject("CLIENT>>> Read OK.");
for (int x = 0; x<buffer.length; x++)
outText.append((char) buffer[x]);
this.jTextArea_ServerResponse.append("\n" + "Client>>>" +outText);
// write buffer to file...
// reset string buffer

Can anyone tell me why this doesn't work and what I need to do to fix it?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Moving to distributed java.

"JavaRanch, where the deer and the Certified play" - David O'Meara
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If you are just transferring the file, you don't want to use readObject and writeObject.
Read with a FileInputStream and have
output as an OutputStream, just:
output.write( buffer, 0, bytes_read )
Why do you want to use these object methods?
I agree. Here's the link:
subject: Transferring a file across a socket connection--HELP
jQuery in Action, 3rd edition