ObjectInput/OutputStream problem

Ok I have been making a little card game, and have started to implement a client/server idea into it.

[CODE]

private void recieveData() throws IOException {

try {

rec = (Packet) input.readObject();

} catch (ClassNotFoundException e) {

System.out.println("Object not a packet");

} catch (SocketException e) {

System.out.println("Socket lost");

//Golden T Game Engine Stuff

parent.nextGameID = Start.TITLE;

finish();

}

}

[\CODE]

That is my recieve code (works same for my client and server). rec is a Packet class defined already.

[CODE]

private void sendData(Packet p) {

try {

output.writeObject(p );

output.flush();

} catch(IOException ioException) {

System.out.println("Error writing message");

}

}

[\CODE]

My Send data code also, it also works the same for my client and server. My streams and connection are working fine. In fact, if I send just a standard string, and not a Packet(my basic class for sending info across the network) and set it to recieve a string(not a packet), it works perfectly fine. But as soon as I use a packet, it gets to the line

rec = (Packet) input.readObject();

in recieveData() and stops running. My packet class looks just like

[CODE]

//Import from Java 1.5

import java.io.Serializable;

//Packet class to send across the network, must be Serializable to be sent over

//network

public class Packet implements Serializable{

//The variables the packet can send

private String message = "";

//Set the message that will be sent

public void setMessage(String aMessage) {

message = aMessage;

}

//Get the message sent over the network

public String getMessage() {

return message;

}

//Write the class as a string

public String toString() {

return message;

}

}

[\CODE]

It implements Serializable, so I cannot figure out the problem. When it hits the line, it does not crash, it just stops at that point. If I take that line out, then it runs fine, and continues on through the whole program, just without receiving data.

I thought maybe I set it up wrong, but everything works to perfection if I use a String and not a Packet like I said before. Anyone have any clue on to why this problem is occuring. It is using a Socket, ObjectInputStream, and ObjectOutputstream for my client(they are all initalized correctly), and a ServerSocket, Socket, ObjectInputStream, and ObjectOutputstream for my server(they also are initalized correctly).

Please Help. Thanks.

[2710 byte] By [TriSwordsa] at [2007-10-1 17:40:08]
# 1

I have had problems similar to this before... in your Serializable object try adding the following two methods (as specified by the Serializable interface):

private void writeObject(java.io.ObjectOutputStream out) throws IOException {

out.defaultWriteObject();

}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

in.defaultReadObject();

}

thatblokea at 2007-7-11 3:58:16 > top of Java-index,Other Topics,Java Game Development...