Ok I have been making a little card game, and have started to implement a client/server idea into it.
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();
}
}
That is my recieve code (works same for my client and server). rec is a Packet class defined already.
private void sendData(Packet p) {
try {
output.writeObject(p );
output.flush();
} catch(IOException ioException) {
System.out.println("Error writing message");
}
}
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
//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;
}
}
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.