java sockets passing objects  
Author Message
crunchyfishstix





PostPosted: 2004-4-27 2:44:00 Top

java-programmer, java sockets passing objects 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{
}
}
 
Christophe Vanfleteren





PostPosted: 2004-4-27 2:50:00 Top

java-programmer >> java sockets passing objects Patrick wrote:

> 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
> {

<snip code>

>
> 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.

--
Kind regards,
Christophe Vanfleteren
 
crunchyfishstix





PostPosted: 2004-4-27 11:33:00 Top

java-programmer >> java sockets passing objects Roedy Green <email***@***.com> wrote in message news:<email***@***.com>...
> On Mon, 26 Apr 2004 18:49:54 GMT, Christophe Vanfleteren
> <email***@***.com> wrote or quoted :
>
> >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.
>

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





PostPosted: 2004-4-27 11:34:00 Top

java-programmer >> java sockets passing objects >
> >
> > 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.

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

~patrick
 
 
crunchyfishstix





PostPosted: 2004-4-28 2:22:00 Top

java-programmer >> java sockets passing objects Roedy Green <email***@***.com> wrote in message news:<email***@***.com>...
> On 26 Apr 2004 20:33:01 -0700, email***@***.com (Patrick)
> wrote or quoted :
>
> >I tried using the defaultRead/WriteObject, when I used those I got a
> >NotSerializable Exception.
>
>
> 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.
 
 
Chris Smith





PostPosted: 2004-4-28 5:45:00 Top

java-programmer >> java sockets passing objects Patrick wrote:
> 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
 
 
Andrew Thompson





PostPosted: 2004-4-28 10:15:00 Top

java-programmer >> java sockets passing objects On Tue, 27 Apr 2004 15:45:16 -0600, Chris Smith wrote:

> 1. The exact source code that you are running to produce this problem.

<http://www.physci.org/codes/sscce.jsp>

> 2. The compiler and version.

(whispers) How do you get
the compiler version?

> 3. The virtual machine and version.

<http://www.physci.org/eg/dos.jsp?text=%0D%0AC%3A%5C%3Ejava+-version%0D%0A%0D%0A>

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
Sudsy





PostPosted: 2004-4-28 12:23:00 Top

java-programmer >> java sockets passing objects Chris Smith wrote:
> Patrick wrote:
>
>>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:

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.

 
 
crunchyfishstix





PostPosted: 2004-4-28 14:01:00 Top

java-programmer >> java sockets passing objects Chris Smith <email***@***.com> wrote in message news:<email***@***.com>...
> 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.

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
 
 
Christophe Vanfleteren





PostPosted: 2004-4-28 15:07:00 Top

java-programmer >> java sockets passing objects Andrew Thompson wrote:

> On Tue, 27 Apr 2004 15:45:16 -0600, Chris Smith wrote:
>
>> 1. The exact source code that you are running to produce this problem.
>
> <http://www.physci.org/codes/sscce.jsp>
>
>> 2. The compiler and version.
>
> (whispers) How do you get
> the compiler version?

javac -version

>
>> 3. The virtual machine and version.
>
>
<http://www.physci.org/eg/dos.jsp?text=%0D%0AC%3A%5C%3Ejava+-version%0D%0A%0D%0A>
>

--
Kind regards,
Christophe Vanfleteren
 
 
Andrew Thompson





PostPosted: 2004-4-28 18:39:00 Top

java-programmer >> java sockets passing objects On Wed, 28 Apr 2004 07:07:10 GMT, Christophe Vanfleteren wrote:

(C.S.)
>>> 2. The compiler and version.
>>
>> (whispers) How do you get
>> the compiler version?
>
> javac -version

Under Java 1.4.2 on XP that reports..
<http://www.physci.org/eg/dos.jsp?text=C%3A%5C%3Ejavac+-version%0D%0Ajavac%3A+invalid+flag%3A+-version%0D%0A>

Are you sure that javac
reports a version at all?

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
Christophe Vanfleteren





PostPosted: 2004-4-28 18:44:00 Top

java-programmer >> java sockets passing objects Andrew Thompson wrote:

> On Wed, 28 Apr 2004 07:07:10 GMT, Christophe Vanfleteren wrote:
>
> (C.S.)
>>>> 2. The compiler and version.
>>>
>>> (whispers) How do you get
>>> the compiler version?
>>
>> javac -version
>
> Under Java 1.4.2 on XP that reports..
>
<http://www.physci.org/eg/dos.jsp?text=C%3A%5C%3Ejavac+-version%0D%0Ajavac%3A+invalid+flag%3A+-version%0D%0A>
>
> 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
...

--
Kind regards,
Christophe Vanfleteren
 
 
Andrew Thompson





PostPosted: 2004-4-28 18:54:00 Top

java-programmer >> java sockets passing objects On Wed, 28 Apr 2004 10:43:56 GMT, Christophe Vanfleteren wrote:
> Andrew Thompson wrote:
>> On Wed, 28 Apr 2004 07:07:10 GMT, Christophe Vanfleteren wrote:
>> (C.S.)
>>>>> 2. The compiler and version.
...
>>> javac -version
...
>> 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:

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..." ;-) ]

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 
 
Chris Smith





PostPosted: 2004-4-28 21:16:00 Top

java-programmer >> java sockets passing objects Patrick wrote:
> 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