UDP sending and receiving (Serializing and Deserializing) object through UDP
Hi,
I'm writing a library to have easy and fast access to methods that I use to need and that handle all errors, auxiliar objects that need to be created, and that stuff, like for file management, UDP Networking, etc.
I'm having problems with my methods for sending UDP serializable objects:
This is the method I use to send:
Code:
public static void UDPSend(Serializable object, InetAddress hostAddress, int destinationPort) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
} catch (IOException ex) {
Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
return;
}
byte[] buf;
try {
out.writeObject(object);
} catch (IOException ex) {
Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
return;
}
try {
out.close();
} catch (IOException ex) {
Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
return;
}
buf = bos.toByteArray();
Networking.UDPSend(buf, hostAddress, destinationPort);
}
The method Code:
Networking.UDPSend(buf, hostAddress, destinationPort);
is tested and works perfectly.
Code:
public static Serializable UDPReceiveSerializable(int port, int timeout) {
ObjectInputStream in = null;
Serializable object;
try {
in = new ObjectInputStream(new ByteArrayInputStream(Networking.UDPReceiveByteArray(port, timeout)));
in.close();
try {
object = (Serializable) in.readObject();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
} catch (IOException ex) {
Logger.getLogger(Networking.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return object;
}
The method Code:
Networking.UDPReceiveByteArray(port, timeout);
is also tested and works perfectly too.
To test this stuff:
1 - I have two copies of the same project.
2 - One of the copies creates, on the main, an object of a class which implements Serializable, and has no access to other local resources (sockets, files), thing that I've been said that could disturb serialization. Then sends the object through the previous method and waits for reply with the another method.
3 - The other copy waits with the second method for a serialized object, gets the object, edits its copy, and sends it to the awaiting method of the first project.
This is why I'm running first the second copy and then, and of course within the timeout interval.
Problem:When I run the second copy, it starts waiting, but when I run the first one, a ClassNotFoundException is thrown in Code:
object = (Serializable) in.readObject();
I don't understand what the hell is happening. And please don't tell me about building issues. I've made sure thousands of times that both projects have different project names, different package names, different classes names, and I perform Clean and Build (Netbeans IDE) before each run.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
Quote:
a ClassNotFoundException is thrown in
Can you post the full text of the error message?
For example, what line in the code and what class was not found?
Where is the definition for the missing class? Is it on the classpath?
BTW your catch block should call the printStackTrace method to get the full text of the error message.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
The whole exception thrown is:
12-ene-2012 13:39:54 packageCommon.Networking UDPReceiveSerializable
GRAVE: null
java.lang.ClassNotFoundException: utilstest.SerializableClassTest
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInput Stream.java:603)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:1574)
at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1495)
at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:350)
at packageCommon.Networking.UDPReceiveSerializable(Ne tworking.java:267)
at utilstest2.UtilsTest2.testUDPReceiveSerializable(U tilsTest2.java:117)
at utilstest2.UtilsTest2.processAndReplySerializable( UtilsTest2.java:123)
at utilstest2.UtilsTest2.main(UtilsTest2.java:19)
It corresponds to the line which contains this: object = (Serializable) in.readObject(); It is thrown in the copy that was waiting (the second) as soon as the first one (supposed to send) begins to run.
If I put the printStackTrace instead the Logger.getLogger stuff, I get this:
12-ene-2012 13:45:31 packageCommon.Networking UDPReceiveSerializable
GRAVE: null
java.lang.ClassNotFoundException: utilstest.SerializableClassTest
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 47)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInput Stream.java:603)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectI nputStream.java:1574)
at java.io.ObjectInputStream.readClassDesc(ObjectInpu tStream.java:1495)
at java.io.ObjectInputStream.readOrdinaryObject(Objec tInputStream.java:1731)
at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1328)
at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:350)
at packageCommon.Networking.UDPReceiveSerializable(Ne tworking.java:268)
at utilstest2.UtilsTest2.testUDPReceiveSerializable(U tilsTest2.java:117)
at utilstest2.UtilsTest2.processAndReplySerializable( UtilsTest2.java:123)
at utilstest2.UtilsTest2.main(UtilsTest2.java:19)
Which, by the way, I think it's the same than before, so I think I'll investigate later the difference between the printStackTrace way and the logger way.
The "missing" class is SerializableClassTest, which makes absolutely non-sense, because actually it is where it should be :
Attachment 2621
Both definitions of it are exactly the same, and here is the code, for if needed.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
The missing class is: utilstest.SerializableClassTest not SerializableClassTest. Notice the package name is part of the class name.
The folder containing the utilstest folder must be on the classpath so the JVM can find the class.
The SerializableClassTest.class file should be in the utilstest folder.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
Pretty pretty curious. The problem is now solved: i've refactored the name of the package of the second copy to utilstest (fom utilstest2) and it now works. That makes sense, since the missing class is utilstest.SerializableClassTest. But ALL renames and that stuff that I do are, always, performed by choosing the proper refactoring option, which is supposed to look for usage-search, refactor in all places where needed, etc., so shouldn't the second copy have tried to use from the beginning utilstest2.SerializableClassTest instead utilstest.SerializableClassTest?
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
Sorry, I have no idea how your IDE works.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
No.
Because you are sending a utilstest.SerializableClassTest.
That is what your serialiser is serialising.
Why would you expect it to try and deserialise it to utilstest2.SerializableClassTest?
As far as Java is concerned they are entirely unrelated.
Re: UDP sending and receiving (Serializing and Deserializing) object through UDP
Quote:
Originally Posted by
Norm
Sorry, I have no idea how your IDE works.
ok, anyway, thanks for the help that solved the problem.
Quote:
Originally Posted by
Tolls
No.
Because you are sending a utilstest.SerializableClassTest.
That is what your serialiser is serialising.
Why would you expect it to try and deserialise it to utilstest2.SerializableClassTest?
As far as Java is concerned they are entirely unrelated.
I see, that clarifies me some doubts on client-server communication and UDP object sending. Thanks.