Bookmark Topic Watch Topic
  • New Topic

Sending file over network through sockets using serializable object  RSS feed

 
Gayatri Shende
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • 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;

}
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
Kindly do not Cross post the same question multiple times.
This is the duplicate
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!