java sockets passing objects

P

Patrick

I have a very simple java application in which I need to pass an
object via sockets. It is an object that I have defined, which has
implemented Serializable. The problem occurs when I send an instance
of the object, when I recieve it on the other end, it is null. The
reciever does in fact recieve an object of the correct type (my user
defined object) but it is empty although the one I sent was not. Is
there something I need to do in the readObject() or writeObject()
methods? Any help would be appreciated, thank you!

For reference, here is my user defined object:

class nodeMessage implements Serializable
{
String text;
int messageID;
int senderID;
int recieverID;
String[] stamp;

public nodeMessage(){}

public nodeMessage(String t, int mID, int sID, int rID,
String[] s)
{
text = t;
messageID = mID;
senderID = sID;
recieverID = rID;
stamp = s;
}

public String getText()
{
return text;
}

public int getMID()
{
return messageID;
}

public int getSID()
{
return senderID;
}

public int getRID()
{
return recieverID;
}

public String[] getStamp()
{
return stamp;
}

public void addStamp(String s)
{
stamp[stamp.length] = s;
}

private void writeObject(java.io_ObjectOutputStream out)
throws IOException{
}
private void readObject(java.io_ObjectInputStream in)
throws IOException, ClassNotFoundException{
}
}
 
C

Christophe Vanfleteren

Patrick said:
I have a very simple java application in which I need to pass an
object via sockets. It is an object that I have defined, which has
implemented Serializable. The problem occurs when I send an instance
of the object, when I recieve it on the other end, it is null. The
reciever does in fact recieve an object of the correct type (my user
defined object) but it is empty although the one I sent was not. Is
there something I need to do in the readObject() or writeObject()
methods? Any help would be appreciated, thank you!

For reference, here is my user defined object:

class nodeMessage implements Serializable
{

private void writeObject(java.io_ObjectOutputStream out)
throws IOException{
}
private void readObject(java.io_ObjectInputStream in)
throws IOException, ClassNotFoundException{
}
}

Just don't put the writeObject/readObject methods in your class unless you
actually do something in them. The default of just implementing
Serializable should work just fine. As it stands now, you don't write your
object to the stream, and you don't read it back in.
 
R

Roedy Green

I have a very simple java application in which I need to pass an
object via sockets.

For sample code see http://mindprod.com/fileio.html

For a start try dumping your object to a file so you can see if it
looks plausible or null.


make sure you are not throwing exceptions away. Catch them and examine
them for clues.
 
R

Roedy Green

Just don't put the writeObject/readObject methods in your class unless you
actually do something in them. The default of just implementing
Serializable should work just fine. As it stands now, you don't write your
object to the stream, and you don't read it back in.

If you do have a read/writeObject they should call defaultReadObject
and writeReadObject and do whatever touch up for the transient fields
are necessary.

see http://mindprod.com/jgloss/serialization.html
 
R

Roedy Green

If you do have a read/writeObject they should call defaultReadObject
and writeReadObject and do whatever touch up for the transient fields
are necessary.

that should read:

If you do have a read/writeObject they should call defaultReadObject
and defaultWriteObject respectively and do whatever touch up for the
transient fields are necessary.

Just look at http://mindprod.com/jgloss/serialization.html
 
P

Patrick

Roedy Green said:
If you do have a read/writeObject they should call defaultReadObject
and writeReadObject and do whatever touch up for the transient fields
are necessary.

I tried using the defaultRead/WriteObject, when I used those I got a
NotSerializable Exception.
 
P

Patrick

Just don't put the writeObject/readObject methods in your class unless you
actually do something in them. The default of just implementing
Serializable should work just fine. As it stands now, you don't write your
object to the stream, and you don't read it back in.

I tried it without putting those functions in, and it gave me a
NotSerializable Exception.

~patrick
 
P

Patrick

Roedy Green said:
Have you read http://mindprod.com/jgloss/serialization.html yet.

I am not going to continue answering questions that are answered
there.

Yes, I did read that, however I couldn't seem to find the answer to my
problem. It states on that site the you DO NOT need to write the
readObject and writeObject methods, however, if I do not include
those, for some reason I get the NotSerializableException when trying
to both write and read. Everywhere I've looked, it states that all I
need to do to make an object serializable is to implement the
interface, but I get that exception when I only do that. If I DO put
those methods in, leaving them empty I can send and recieve the
object, but everything is null or 0 when I recieve it. My only members
are Strings, ints, and a String array, unless these are inheritly
transient for some reason, they should be restored, but they aren't. I
thank you for your help, and the link you sent me was very
informative, however I couldn't find the reasoning for my problems
within it unless I'm missing something subtle.
 
R

Roedy Green

Everywhere I've looked, it states that all I
need to do to make an object serializable is to implement the
interface, but I get that exception when I only do that.

Implementing an interface means two things:

1. adding a line implements xxxx at the top
2. writing the necessary methods.
 
C

Chris Smith

Patrick said:
It states on that site the you DO NOT need to write the
readObject and writeObject methods, however, if I do not include
those, for some reason I get the NotSerializableException when trying
to both write and read.

There's no explanation for this. If I take your original code and
remove the writeObject and readObject methods, and then try to serialize
it, it works fine. So, we need to figure out what you're doing
differently. Please let us know:

1. The exact source code that you are running to produce this problem.
2. The compiler and version.
3. The virtual machine and version.
If I DO put those methods in, leaving them empty I can send and recieve
the object, but everything is null or 0 when I recieve it.

If you do put this methods in, then you're overriding the default
serialization. If you do that, then you need to write code in those
methods, or else you've overridden serialization to become non-
functional. There's not even a chance that'll work; so let's stay
focused on something that should be working, and solve that problem.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Sudsy

Chris said:
There's no explanation for this. If I take your original code and
remove the writeObject and readObject methods, and then try to serialize
it, it works fine. So, we need to figure out what you're doing
differently. Please let us know:

Like Chris, I also did a bit of cut-and-paste since the code is so
standard that I couldn't believe that it wasn't working. This code
works exactly as expected:

import java.io.*;

public class NodeTest {

private static final String FILENAME = "/tmp/junkfile";

public static void main( String args[] ) {
nodeMessage app = new nodeMessage();
ObjectOutputStream oos = null;
ObjectInputStream ois = null;

try {
app.setText( "testing" );
oos = new ObjectOutputStream(
new FileOutputStream( FILENAME ) );
oos.writeObject( app );
oos.close();
ois = new ObjectInputStream(
new FileInputStream( FILENAME ) );
app = (nodeMessage) ois.readObject();
ois.close();
System.out.println( app.getText() );
}
catch( Exception e ) {
e.printStackTrace();
System.exit( 12 );
}
}
}

class nodeMessage implements Serializable
{
String text;
int messageID;
int senderID;
int recieverID;
String[] stamp;

public nodeMessage(){}

public nodeMessage(String t, int mID, int sID, int rID,
String[] s)
{
text = t;
messageID = mID;
senderID = sID;
recieverID = rID;
stamp = s;
}

// I added this method for testing with the no-argument
// constructor (see main method)

public void setText( String s ) {
this.text = s;
}

public String getText()
{
return text;
}

public int getMID()
{
return messageID;
}

public int getSID()
{
return senderID;
}

public int getRID()
{
return recieverID;
}

public String[] getStamp()
{
return stamp;
}

public void addStamp(String s)
{
stamp[stamp.length] = s;
}
/*
private void writeObject(java.io_ObjectOutputStream out)
throws IOException{
}
private void readObject(java.io_ObjectInputStream in)
throws IOException, ClassNotFoundException{
}
*/
}

So what's the problem? Cut and paste this code, replace FILENAME with
something appropriate to your platform and post the results.
 
P

Patrick

Chris Smith said:
There's no explanation for this. If I take your original code and
remove the writeObject and readObject methods, and then try to serialize
it, it works fine. So, we need to figure out what you're doing
differently. Please let us know:

1. The exact source code that you are running to produce this problem.
2. The compiler and version.
3. The virtual machine and version.


If you do put this methods in, then you're overriding the default
serialization. If you do that, then you need to write code in those
methods, or else you've overridden serialization to become non-
functional. There's not even a chance that'll work; so let's stay
focused on something that should be working, and solve that problem.

Thank you all for your help, but I have found the problem. I had
written my message class as an interior class of the class that was
trying to use it since the message class itself wasn't very large.
This is what was causing the problem, since the class it was contained
in was itself not serializable. Once I created the message object in
its own file, it worked fine. Thinking about it now, it seems obvious,
however, at the time (and since this is my first time using/creating
serializable objects) it never occured to me. And to the gentleman who
was giving me links to his essays, that may be a small and subtle
caveat you may want to add, though I'm not sure if writing interior
classes is 'proper' or done by many, it may just be my coding style.
Thank you again for all of your time and help!

!patrick
 
C

Christophe Vanfleteren

Andrew said:
On Wed, 28 Apr 2004 07:07:10 GMT, Christophe Vanfleteren wrote:

(C.S.)

Under Java 1.4.2 on XP that reports..
Are you sure that javac
reports a version at all?

You are correct, I get the same result with 1.4.2 on Linux.
But the 1.5 beta javac does recognise the version flag:

cxvx@luke cxvx $ javac -version
javac 1.5.0-beta
....
 
A

Andrew Thompson

....
You are correct, I get the same result with 1.4.2 on Linux.
But the 1.5 beta javac does recognise the version flag:

Aah! I had installed the 1.5 JRE (just
to grab the rt.jar) but have not bothered
to download the SDK yet.

[ "The *marvels* of modern technology!
Getting the version straight from
javac, who would've figured..." ;-) ]
 
C

Chris Smith

Patrick said:
Thank you all for your help, but I have found the problem. I had
written my message class as an interior class of the class that was
trying to use it since the message class itself wasn't very large.

Ah, okay. Yes, an inner class would do that. On the other hand, a
static nested class won't have the same problem. So if you still want
your message class declared inside a containing class, just add
'static' to its declaration.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top