By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Sending Files over Network (ObjectStream)

P: n/a
Hi Group!

I am just working on a client-server network application which
exchanges different Message Classes. (ObjectInputStream and
ObjectOutputStream)
Now i also would like to send a file over this connection

objOut.writeObject(new File("hi,exe"));

doesn't work (I just send a reference to this file)

What is the best way to do this? I think i also can't send the Input
Stream as Object, sending and receive byte by byte doesn't work either
(Get EOFException before file is enden with this example

//dis is datainputstream
byte b;

try {
while((b=dis.readByte())!=-1) {
objOut.write(b);
}
}
It would be great if you could help!

Gabriel
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Gabriel Gudenus" <ga*****@gudenus.com> wrote in message
news:f2*************************@posting.google.co m...
Hi Group!

I am just working on a client-server network application
which exchanges different Message Classes.

Ok, you are using [or must use because it is a requirement ?] these stream
classes:

* ObjectInputStream
* ObjectOutputStream

to exchange objects between two applications ? I'm assuming you are aware of
the need for such objects to be 'Serializble', etc.

Now i also would like to send a file over this connection

objOut.writeObject(new File("hi,exe"));

doesn't work (I just send a reference to this file)

The important question, here, is: What do you want to send ?

* A 'File' object ? Or,

* The contents [bytes comprising] of the file, 'hi.exe' ?

If the former, I hope you realise that the information contained in the
object may not be valid when that object is reconstituted at the receiving
end [i.e. the path information for 'hi.exe' may not be valid there].

On the other hand, if the latter, the easiest approach would probably be to
create a Serializable class containing a 'byte[]', read the contents of
'hi.exe' into it, and send this object off. The receiver will then
reconstitute this object, and perhaps recreate 'hi.exe'.

There are, of course, much easier ways for applications of exhange data;
these answers, however, assume you may only use 'ObjectInputStream' and
'ObjectOutputStream' to do so.

What is the best way to do this ? I think i also can't send
the Input Stream as Object,

Stream objects, like 'InputStream'-based objects, are not 'Serializable', so
cannot be sent as objects in this way. Also, they are the type of transitory
object that *shouldn't* be serialized anyway.

sending and receive byte by byte doesn't work either
(Get EOFException before file is enden with this example

//dis is datainputstream
byte b;

try {
while((b=dis.readByte())!=-1) {
objOut.write(b);
}
}
It would be great if you could help!


I suspect you need a class wrapping up a 'byte[]' as I suggested earlier.
Give it a try - at worst it can only not work ;) !

I hope this helps.

Anthony Borla
Jul 17 '05 #2

P: n/a
"Anthony Borla" <aj*****@bigpond.com> wrote in message news:<Fp*****************@news-server.bigpond.net.au>...
"Gabriel Gudenus" <ga*****@gudenus.com> wrote in message
news:f2*************************@posting.google.co m...

The important question, here, is: What do you want to send ?

* A 'File' object ? Or,

* The contents [bytes comprising] of the file, 'hi.exe' ?

I wanted to exchange the contents (so whole file except path and
filename info)
On the other hand, if the latter, the easiest approach would probably be to
create a Serializable class containing a 'byte[]', read the contents of
'hi.exe' into it, and send this object off. The receiver will then
reconstitute this object, and perhaps recreate 'hi.exe'.


Was a great Idea! I just used it for Demo Purposes with the Byte class
and worked really good (except OutOfMemory Problem at files bigger 2
Meg)

Here the solution:

//Client I know that i receive a file now because of message before
//fos = FileOutputStream

o = objIn.readObject();
while (o instanceof Byte) { fos.write(((Byte) o).byteValue());
o = objIn.readObject();
}
Thanks for your Help!

Gabriel
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.