Win a copy of Terraform in Action this week in the Cloud forum!
    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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Sending file over network through sockets using serializable object

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi
I am writing a code to transfer a file from server machine to client machine.
I am using Object protocol for communication. ie client sends a filerequest object , server recognizes it and send the response back in form of object again.
My problem is :

I read the file on server machine , in chunks of 1024 bytes.
I try to write an object to Objectoutputstram of socket.
For doing this, If I store the data read from file in any outputstream/Bufferedoutputstream etc then it throws exception : NOTserializable. Please guide me, what should I use to store the data from file and write it as object.

Here is my code:

Server:


ObjectOutputStream objOut = new ObjectOutputStream (socket.getoutputStream());

objOut.writeObj(processfile());
objOut.flush();



public Object processfile()
{

byte[] packet = new byte[1024];
int bytes_trans=0; //transfered bytes
int bytes_read;//read bytes
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out;

try {
FileInputStream fin = new FileInputStream(filename);



while( (bytes_read = fin.read(packet, 0,packet.length)) >= 0)
{
bout.write(packet,0,bytes_read);

bytes_trans = bytes_trans + bytes_read;
System.out.println("Total bytes transferred :"+ bytes_trans);
}
System.out.println("Done! ");
//bout.flush();
//out = new ObjectOutputStream(bout);
//out.writeObject(out);

//fin.close();

//return out;

}
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Kindly do not Cross post the same question multiple times.
This is the duplicate
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic