954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

can objects be passed through sockets

I've revived an old java project I did in college. Basic chat type program like ICQ which stores offline messages and users in a database etc, etc.

I've recently switched the database from Access to MySQL and am considering some other alterations.

I've been reading a bit on RMI. I guess what I'd like to know is can I pass more than just string across the sockets? The program uses a BufferedReader and I just built it to pass delimited strings which both the client and server parsed on either end.

I'm not sure if java supports user-defined-types either, but I guess thats basically what I'd like to pass back and forth just to make things easier.

agrothe
Junior Poster
153 posts since Jun 2006
Reputation Points: 37
Solved Threads: 18
 

As long as the object properly implements serialisable then you can send it through an ObjectOutputStream wrapped around the OutputStream retreived through socket.getOutputStream, and read through an ObjectInputStream wrapped around the receiving ends socket.getInputStream.

And, of course, RMI and CORBA are other possibilites as long as you define your stubs correctly. Sun provides an RMI tutorial here http://java.sun.com/docs/books/tutorial/rmi/index.html

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Thanks, I'll look more into wrapping the ObjectOutputStream. RMI and CORBA sound great by SUNs tutorials, but I think for something simple as what I'm trying to accomplish they will add too much baggage and just complicate things needless. I'd rather the lower amount of overhead possible.

agrothe
Junior Poster
153 posts since Jun 2006
Reputation Points: 37
Solved Threads: 18
 

RMI in fact makes working with remote objects a lot easier than doing the entire stuff of serialisation, transmission, and receiving yourself.

The actual overhead on the client is quite limited, little more than it would be if you were to establish a socket connection to the server (and the server becomes a lot easier to write).

The only disadvantage (which you'd also have if you're serialising objects yourself) is that it won't be portable to other platforms without serious effort.
Only platform agnostic systems like SOAP and Corba provide for that out of the box.

A complete mulithreaded server that will provide a single RMI service (Remote object) can be coded in about 10KB of Java code (not counting the Remote object itself, which is just a Java bean with some special rules) and about half of that's comments and logging code.
Those are real figures, figures from an RMI application I created for my certification (currently on hold still awaiting funds for the final exam).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
The only disadvantage (which you'd also have if you're serialising objects yourself) is that it won't be portable to other platforms without serious effort. Only platform agnostic systems like SOAP and Corba provide for that out of the box.

So what your saying is, if I want maximum portability and to run effortlessly on any platform, I should just stick with sockets and continue parsing strings?

I guess I could just brake an object down to a delimited string, transmit through socket, and then rebuild on the other end.... Or is Corba worth learning for this wanted (read: not needed) functionality?

agrothe
Junior Poster
153 posts since Jun 2006
Reputation Points: 37
Solved Threads: 18
 

Depends on what you want.
If communication with non-Java systems is required, you will need to pass your data in a way that those platforms can understand.
The easiest way to do that is to use some protocol based on flat text with no binary data whatsoever.

Corba is hardly ever used anymore, been completely superceded by SOAP.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
Corba is hardly ever used anymore, been completely superceded by SOAP.

From Wikipedia:SOAP is a protocol for exchanging XML -based messages over computer networks , normally using HTTP / HTTPS . SOAP forms the foundation layer of the Web services stack , providing a basic messaging framework that more abstract layers can build on.

Looks like I need to do some reading on this, as this is very similar to what I'm trying to accomplish.

Thanks jwenting, and by the way, do you have a life outside these forums? lol :)

agrothe
Junior Poster
153 posts since Jun 2006
Reputation Points: 37
Solved Threads: 18
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You