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
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
Normally one writes one's code so that the receiving end knows
what's coming. Any reason not to do that?
Marshall
"Marshall Spight" <msp...@dnai.com> wrote in message news:<RplF7.21897$wj5.11...@news1.rdc1.sfba.home.com>...
> 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.
n...@for.email (Gordon Beaton) wrote in message news:<9s47mj$5us$1...@news.du.uab.ericsson.se>...
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.
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