Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to recognize the class transmitted through TCP socket using Serializable.

0 views
Skip to first unread message

Kening Ren

unread,
Nov 4, 2001, 3:06:11 PM11/4/01
to
Hi, Java Guru:
I am not trying to use Serializable to transmit the class through TCP
socket. But I have a problem.
I need to transmit serveral different kinds of classes (or instances,
more accurately) to the other end of a TCP socket. But I do not know
how to make the other end to recognize which types these classes are.
Here is my code in the other end:

import java.io.*;
import java.net.*;

/**
* Reads object from socket
*/
public class SocketServer
{
public static void main(String args[])
{

int port = 3500;

while(true)
{
try
{
ServerSocket ss = new ServerSocket(port);
Socket so = ss.accept();

ObjectInputStream in
= new ObjectInputStream(so.getInputStream());
Object obj1 = null;
Object obj = null;
while (true) {
obj1 = in.readObject();
try {
obj = obj1.getClass().newInstance();
} catch (InstantiationException Ie) {
Ie.printStackTrace();
} catch (IllegalAccessException IAe) {
IAe.printStackTrace();
}

/*Info and Info2 are two different classes.*/
/*Info has methods: getName() and gerAge()*/
/*Info2 has methods: getName() and gerGender()*/
if (obj instanceof Info) {

System.out.print(((Info) obj).getName() + ": ");
System.out.println(((Info) obj).getAge());

} else if (obj instanceof Info2) {
System.out.print(((Info2) obj).getName() + ": ");
System.out.println(((Info2) obj).getGender());

} else {
System.out.println("Unknown object received!");
break;
}
}


in.close();

so.close();
ss.close();
}
catch (IOException e){
e.printStackTrace();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
}

The error is:

bash-2.04$ java SocketServer
java.lang.InstantiationException: [LInfo;
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Class.java:237)
at SocketServer.main(SocketServer.java:33)
Unknown object received!

Any ideas?

BR
Kening

Gordon Beaton

unread,
Nov 4, 2001, 3:16:19 PM11/4/01
to
On 4 Nov 2001 12:06:11 -0800, Kening Ren wrote:
> The error is:
>
> bash-2.04$ java SocketServer
> java.lang.InstantiationException: [LInfo;
> at java.lang.Class.newInstance0(Native Method)
> at java.lang.Class.newInstance(Class.java:237)
> at SocketServer.main(SocketServer.java:33)
> Unknown object received!
>
> Any ideas?

It looks like you've sent an Info[] (i.e. an array), not an Info.

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

Marshall Spight

unread,
Nov 4, 2001, 7:53:05 PM11/4/01
to
"Kening Ren" <progra...@yahoo.com> wrote in message news:a69534dc.01110...@posting.google.com...

> Hi, Java Guru:
> I am not trying to use Serializable to transmit the class through TCP
> socket. But I have a problem.
> I need to transmit serveral different kinds of classes (or instances,
> more accurately) to the other end of a TCP socket. But I do not know
> how to make the other end to recognize which types these classes are.
> Here is my code in the other end:

Normally one writes one's code so that the receiving end knows
what's coming. Any reason not to do that?


Marshall

Kening Ren

unread,
Nov 5, 2001, 2:25:45 AM11/5/01
to
Hi,
The source needs to send two kinds of classes. But the other end needs
to check which type of the two classes the received object is. So I
think there must be some methods to deal with it. This is what I am
asking.
Any solutions?
--Kening


"Marshall Spight" <msp...@dnai.com> wrote in message news:<RplF7.21897$wj5.11...@news1.rdc1.sfba.home.com>...

Gordon Beaton

unread,
Nov 5, 2001, 2:32:54 AM11/5/01
to
On 4 Nov 2001 23:25:45 -0800, Kening Ren wrote:
> The source needs to send two kinds of classes. But the other end
> needs to check which type of the two classes the received object is.
> So I think there must be some methods to deal with it. This is what
> I am asking.

> Any solutions?

In addition to my earlier answer, the receiving process must have
access to the class definitions somewhere in its classpath, or it will
be unable to create the objects.

If neither this nor my previous response are enough, then you also
need to post the code where you send the objects.

Kening Ren

unread,
Nov 5, 2001, 2:35:11 AM11/5/01
to
Hi.
Yes you are right. But when I change it to send only one class ( not a
class array ), I still get the same result.
I know InstantiationException means I try to use obj or obj1 to get
the instance, but they are only interface, so JVM can not do it.
I need to find a solution to problem.
--Kening


n...@for.email (Gordon Beaton) wrote in message news:<9s47mj$5us$1...@news.du.uab.ericsson.se>...

Steve Horsley

unread,
Nov 5, 2001, 4:07:13 AM11/5/01
to
n...@for.email (Gordon Beaton) wrote in message news:<9s47mj$5us$1...@news.du.uab.ericsson.se>...
> On 4 Nov 2001 12:06:11 -0800, Kening Ren wrote:
> > The error is:
> >
> > bash-2.04$ java SocketServer
> > java.lang.InstantiationException: [LInfo;
> > at java.lang.Class.newInstance0(Native Method)
> > at java.lang.Class.newInstance(Class.java:237)
> > at SocketServer.main(SocketServer.java:33)
> > Unknown object received!
> >
> > Any ideas?
>
> It looks like you've sent an Info[] (i.e. an array), not an Info.
>
> /gordon

Two other points:

1) By using ObjectInputStream and ObjectOutputStream, you ARE using
Serializable, despite your claims not to be.

2) It would seem that your server cannot find the Info.class file in its
local file system. This is the cause of the error you see. It's receiving
a class of a type it doesn't know about.

3) Beware of the "funnies" of Object streams. You need to understand the
way they cache objects sent and received, and that re-sending an object
simply refers to the original cache entry, even if the object's fields
have changed. Check out the Reset method.

Guess I can't count. Hope this helps. Steve.

Marshall Spight

unread,
Nov 5, 2001, 11:05:41 AM11/5/01
to
"Kening Ren" <progra...@yahoo.com> wrote in message news:a69534dc.01110...@posting.google.com...
> Hi,
> The source needs to send two kinds of classes. But the other end needs
> to check which type of the two classes the received object is. So I
> think there must be some methods to deal with it. This is what I am
> asking.
> Any solutions?

Does it send first one, then the other? In that case you know
which order they came in. If it's one, or the other, willy-nilly,
with no regularity, that's perhaps not the best design.

Another solution would be to have the two classes implement
a common interface, or have a common parent class, and use
them polymorphically.

Worst case, you can still use "instanceof" to see which one it
is. Note that this is usually a sign of poor design. But it works!


if (obj instanceof MyClass1)
// code
else if (obj instanceof MyClass2)
// more code


HTH


Marshall

0 new messages