problem getting enum through Socket

R

robot.dancing

Hi,

I have to use Java version 1.4.1 and I need to use enum. (Not really
need to, but it would make my life easier later on).

We all know that enum is not supported in pre-1.4.2 versions, and
there is a realy nice example of how to implement it at
http://www.javaworld.com/javaworld/javatips/jw-javatip133.html.

The problem is that, even though I can get this method to work with
writing and reading to the disk, it fails abysmally when trying to use
it with sockets at .getInputStream().

Has anyone tried creating something like this and sending it via a
socket?

What I am trying to do:

Send a class Class1 from one PC to another.
class structure is:

enum A1
Hashtable H1
Hashtable H2

Can someone give me a pointer on how to get it working?
Thanks!
 
L

Lew

The problem is that, even though I can get this method to work with
writing and reading to the disk, it fails abysmally when trying to use
it with sockets at .getInputStream().

Has anyone tried creating something like this and sending it via a
socket?

What I am trying to do:

Send a class Class1 from one PC to another.
class structure is:

enum A1
Hashtable H1
Hashtable H2

I am curious, what exactly do you mean by "send a class" - send its bytecode?

What advantage does "sending a class" provide that sending a parameter wouldn't?

Are you serializing and deserializing objects (not classes) between JVMs? If
so, be aware that that is always risky.
 
T

Tom Hawtin

We all know that enum is not supported in pre-1.4.2 versions, and
there is a realy nice example of how to implement it at
http://www.javaworld.com/javaworld/javatips/jw-javatip133.html.

The problem is that, even though I can get this method to work with
writing and reading to the disk, it fails abysmally when trying to use
it with sockets at .getInputStream().

One sequence of octets is much like another. What exactly was the
result, and do you have a small, complete, compilable example thing?

There's a few things that obviously wrong with the JavaWorld code.

* No explicit serialVersionUID is declared (both classes). Are you
sure your classes are in sync?
* readObject doesn't start with either defaultReadObject or readFields
(and equivalent for writeObject).
* writeObject use getDeclaredFields (but readObject does not...). This
means if any of your constants is defined as a subclass (probably an
anonymous inner class), it will fail. Also there may be security problems.
* readResolve should be protected at worst.
* If you do use subclasses within the enum, serialisation will be
messed up. Better to use writeReplace in AbstractConstant to substitute
an enum-neutral object with field name and enum class.
* toString should be overriden.

Tom Hawtin
 
E

Esmond Pitt

The problem is that, even though I can get this method to work with
writing and reading to the disk, it fails abysmally when trying to use
it with sockets at .getInputStream().

Fails abysmally *how*? That particular method can only fail if the
socket is already closed, and it has nothing to do with sending. Not
that much to do with receiving either, except that calling it is a
precondition.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top