I am having a problem with sending two objects (over a socket). I have read in other posts that this could be due to trying to receive incompatible data types but my applications work fine if I send my objects synchronously rather than asynchronously.
I will try my best to describe what my problem is as my code is very long.
I have a server and a client application (2 apps). Multiple clients connect to the server and send their details (as an object) to the server. The server then amends the object (adds some more data) and sends it back to the clients. Both the SendObject and ReceiveObject class are threads and I have created a Listener (within the client) that activates when an object is received (asynchronous communication). The Listener method looks to see if the event is an instance of a particular class and casts is as appropriate (as per below).
public void receivedObject(ReceivedObjectEvent e) {
ReceiveObjectThread obj = (ReceiveObjectThread) e.getObject();
if(obj.getObject() instanceof Player) {
thePlayer = (Player) obj.getObject();
theTable.setHandData(thePlayer.getHand());
}
if(obj.getObject() instanceof GameData) {
gameData = (GameData) obj.getObject();
theTable.setPlayerList(gameData.getOpponents());
}
}
The objects that are passed between applications both implement Serializable.
This all works fine synchronously object passing. However, if I try and spawn two sendObject threads within the server and the corresponding two receive threads within the client and wait for the Listener to activate (asynchronously) I get the following error:
java.io.StreamCorruptedException: invalid stream header: 00057372
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at ReceiveObjectThread.run(ReceiveObjectThread.java:84)
java.io.StreamCorruptedException: invalid stream header: ACED0006
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at ReceiveObjectThread.run(ReceiveObjectThread.java:84)
I am sure that this problem is due to my limited knowledge on socket and data transfer. Therefore any help on this one will be gratefully received.