Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transferring a file across a socket connection--HELP

 
Mike London
Ranch Hand
Posts: 1208
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to distributed java.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic