*
The moose likes Sockets and Internet Protocols and the fly likes About ObjectOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "About ObjectOutputStream" Watch "About ObjectOutputStream" New topic
Author

About ObjectOutputStream

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
A simple question :

Given two TCP sockets, can one invocation of writeObject(Object o) on one end result in multiple invocations of readObject() in the other?

(I have read this doesn't happen when you're testing locally)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

No. One writeObject() corresponds to one readObject().

marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
Ok this leads me to another question, why do network programmers bother writing protocols to map objects to their byte representation? Can't all of it be skipped by making network stuff implement Serializable then relying writeObject() and readObject()? Also, can I use these two methods with NIO? All the NIO examples I've read use ByteBuffers.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

marwen Bakkar wrote:Ok this leads me to another question, why do network programmers bother writing protocols to map objects to their byte representation? Can't all of it be skipped by making network stuff implement Serializable then relying writeObject() and readObject()?


Because Java's built-in serialization, while a reasonable general case mechanism for Java-to-Java communication, is not appropriate in all use cases. Sometimes we need it to be human readable, or portable to other platforms, so we may use XML (such as provided by XStream or javabeans) or some other representation. Sometimes we may have specific performance needs in terms of space or size for which the built-in mechanism is not up to the task, so we might write our own protocol that's geared toward our needs.

Also, can I use these two methods with NIO? All the NIO examples I've read use ByteBuffers.


I don't know. I've never really used NIO, so I don't know what it provides. I expect there are streams though, and if there are, you can wrap then in OOS/OIS.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

NIO works with channels, not with streams. However, you can turn several channels into streams by using the static methods of java.nio.channel.Channels. For instance, to get an InputStream for a SocketChannel all you need to do is call Channels.new InputStream(socketChannel). In a similar way you can get an OutputStream.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marwen Bakkar
Ranch Hand

Joined: Jan 28, 2010
Posts: 84
SocketChannel has socket() method that returns its socket. I wonder if it's possible to test for readiness selection then instead of reading bytes I get an OIS and use readObject().

What I want to do is instead of constructing packets of the sort :

content length, content type, data

I wrap the data bytes inside a serialization proxy with a single byte array field and instead of writing the raw bytes to the stream I'd write the proxy with writeObject(). Wouldn't this avoid the need of for the headers and buffering on the other side until we get a meaningful sequence of bytes? (Since writeObject() always results in a single readObject())
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That's certainly one way of producing a simple communications protocol. As long as you only want to send byte arrays, and you don't mind materializing the whole byte array in memory at both client and server, that is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About ObjectOutputStream
 
Similar Threads
nanananananana
some general queries
WA #2 ..... word association
i am a green horn now, how can i get upgrade?
Unidirectional & Bi directional