• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transferring a file across a socket connection--HELP

 
Bartender
Posts: 1971
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to distributed java.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Evildoers! Eat my justice! And this tiny ad's justice too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic