Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Sending Object through TCP/IP...  (Read 3456 times)
0 Members and 1 Guest are viewing this topic.
Offline LordWilson

Junior Newbie




Who runs it???...TKE Runs it...


« Posted 2003-04-23 20:40:08 »

I am trying to send a Deck object that I have created by subclassing the Vector class through a TCP/IP Connection...I'm not sure if i'm doing this correctly, however...the problem isn't getting the deck SENT, i don't think, but receiving it correctly is the problem...

The Client side code is as follows...

InetAddress server = InetAddress.getByName("hippo");
                 int port = 22222;
                 
                 Socket socket = new Socket(server, port);
                 
                 System.out.println("Connected to server...attempting to send deck");
                 
                 ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                 oos.writeObject(deck);
                 System.out.println("DeckSent");
                 oos.flush();
                 oos.close();
socket.close();

(I'm catching everything necessary...)

Please don't poke too much fun at the code, i'm just beginning this TCP/IP stuff ;-)

anyways, i'm sure that the message is getting to the server...because the connection is made fine...

server code is as follows...

int servPort = 22222;
             ServerSocket serverSocket = new ServerSocket(servPort);
             
             while(true){
                   Socket inSocket = serverSocket.accept();
                   
                   ObjectInputStream ois = new ObjectInputStream(inSocket.getInputStream());
                   
                   Object possibleDeck = ois.readObject();
                       System.out.println("Checking for Instance");
                       if(possibleDeck instanceof Deck){
                             System.out.println("It's a Deck");
                             deck = (Deck)possibleDeck;                        
                             
                             System.out.println("Deck Loaded..."      );
                       }
                       else
                             System.out.println("It's not a deck");
                       ois.close();
                       serverSocket.close();      
                 }

it gives the error message (which i will display in a sec) when it reaches the line:  "Object possibleDeck = ois.readObject();"

The error message is as follows...

java.io.InvalidClassException: javax.swing.JComponent; local class incompatible:
stream classdesc serialVersionUID = 4775608949283930591, local class serialVers
ionUID = -7908749299918704233

I can't seem to fix it...it might help to know if where the problem is rising...is it the WAY that i'm sending the object (basically, CAN I DO IT THAT WAY????)...or does it have to do with something else?...

any help is appreciated...even suggestions on how to fix it (sample code is *always* welcome...) ...

if i'm not sending the object in the correct manner, what is the best (easiest, even) to send one object from one machine to another?...

Thanx for any help....

Russ

Don't talk...You'll seem less stupid that way...
Offline mill

Junior Member




popcorn freak


« Reply #1 - Posted 2003-04-24 12:17:09 »

Vector itselt is serializable, but you may have objects in your Deck that are not or perhaps you have attributes which aren't.

try making your attributes transient and see if that works.

if objects aren't serializable i think you get another error message than the one you get (if i remember correctly)

anyway, from the error message it looks like the object sent is not the same kind as the one that's received.

you are using the same class file, right? as in you haven't recompiled the client's Deck-file or something?

Offline LordWilson

Junior Newbie




Who runs it???...TKE Runs it...


« Reply #2 - Posted 2003-04-25 02:36:30 »

I am using the same classes...

I think i might be able to narrow it down, however...

it says the exception is coming from javax.swing.Jcomponent...a Deck object is basically a subclass of a Vector (perfectly serializable, btw)...the Deck contains any number of Card objects...Cards are basically just subclasses of a JButton...now, a JButton is a JComponent, so I'm pretty sure that the problem lies within that class...however, I'm not sure how serialization works with hierarchy...

in order to make the classes compatible, i have set the serialVersionUID in both the Deck class and the Card class...

(...Deck...)
static final long serialVersionUID = -8436333800348538015L;

(...Card...)
static final long serialVersionUID = -8293881053128968132L;

as you can see, those SVUIDs aren't either of the ones described in the exception...

does anyone have a clue as to what I should do to fix this problem???...has anyone run into this before?...

Thanx for any input...

Russ


Don't talk...You'll seem less stupid that way...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #3 - Posted 2003-04-25 10:04:16 »

What are the JVMs at either end of the connection?

Hellomynameis Charlie Dobbie.
Offline coilcore

Senior Newbie




Java games rock!


« Reply #4 - Posted 2003-04-25 19:34:13 »

When you serialize an object every member within that object is also serailized, and each of any members objects are serialized, and so on.  The way to prevent this is to mark a member as 'transient' this means it will not be serialized and therefore will not be in your data you send across the wire.  e.g.

 public ArrayList a = new ArrayList();
 public transient ArrayList b = new ArrayList();

The deserialized object ArrayList a will have all the contents from the original object, but b will be empty even if the serialized class had data there.

Clearly you are trying to deserialize something thats not the same between each JVM (either by your own classes or JVM version).  I'd suggest starting near the top and narrowing as you go making objects transient to try to isolate the problem.  

Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #5 - Posted 2003-04-26 20:11:32 »

Quote

The error message is as follows...

java.io.InvalidClassException: javax.swing.JComponent; local class incompatible:


In general, NONE of the gui classes are serializable. Many AWT classes are inherently non-serializable (I've heard before that this is because, under the hood, they contain references to things like your local graphics memory; Sun could still have created a workaround, but apparently haven't). Nearly all Swing classes have for a while come with the comment (written in the docs for every class - have you READ the docs for the classes you are using?) "blah blah this is NOT serializable between different VM versions and/or platforms...this will be fixed in a later version of  swing blah blah etc".

In general, just never try to serialize a Swing class. Although I do feel your pain - sometimes (though I very much doubt this is the case for you) you have to serialize gui classes. However, why on earth do you want to send a Button over the network? Just obey the M/V/C architecture (simple concept: separate your data (Model), display (View) and mouse-handling etc (Controller) - M,V,C. Read the Swing docs for info) and make a Model class representing the underlying data of your cards, and send that.

Have it DISPLAYED both ends by a separate class (a View, from the M/V/C terminology).

If this isn't clear, let me know and I'll try again; been awake almost 20 hours, and I'm not thinking too straight Smiley.

malloc will be first against the wall when the revolution comes...
Offline LordWilson

Junior Newbie




Who runs it???...TKE Runs it...


« Reply #6 - Posted 2003-04-26 23:18:53 »

OK...so, I've managed to get the Deck successfully deserialized...that's the good news...

the bad news...is that I had to be using the same version of the JVM on both sides (precisely as all of you had implied)...and that's not taking advantage of the platform independency that java is used for...SO...I will be taking the advice of mr. blahblahblahh and using the M/V/C technique...

I guess i had to learn the lesson of swing serialization the hard way...

thanx for helping me out, fellas...I appreciate it...

Russ

Don't talk...You'll seem less stupid that way...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (19 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (31 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!