wood burning stoves 2.0*
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
Author

Transferring a file across a socket connection--HELP

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
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 = from.read(buffer);
output.writeObject(buffer);
output.flush();
for (int x = 0; x < buffer.length; x++)
{
outText.append((char) buffer[x]);
}
System.out.println("This read:\n" + outText);
outText.setLength(0);
// wait until client is ready for more.
do
{
// nothing...loop...
}
while (!input.readObject().equals("CLIENT>>> Read OK."));
} while (bytes_read != -1);
==============================================
The client code looks like this:
===============================================
do
{
try
{
buffer = (byte[]) input.readObject();
output.writeObject("CLIENT>>> Read OK.");
output.flush();
for (int x = 0; x<buffer.length; x++)
{
outText.append((char) buffer[x]);
}
this.jTextArea_ServerResponse.append("\n" + "Client>>>" +outText);
// write buffer to file...
out.write(buffer);
// reset string buffer
outText.setLength(0);
}

=======================================
Can anyone tell me why this doesn't work and what I need to do to fix it?
THANKS MUCH IN ADVANCE!!!
--Mike
Cindy Glass
"The Hood"
Sheriff

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
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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?
Bill
 
wood burning stoves
 
subject: Transferring a file across a socket connection--HELP
 
Similar Threads
Transferring file name then file data over socket
Sending file over network through sockets using serializable object
Problem while transferrinf file over network thru serializable object
how can i copy a file from one location to another location then delete............
Writing to files using socket server