Ok I have been making a little card game, and have started to implement a client/server idea into it.
1 2 3 4 5 6 7 8 9 10 11 12
| 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"); parent.nextGameID = Start.TITLE; finish(); } } |
That is my recieve code (works same for my client and server). rec is a Packet class defined already.
1 2 3 4 5 6 7 8
| 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.io.Serializable;
public class Packet implements Serializable{
private String message = "";
public void setMessage(String aMessage) { message = aMessage; }
public String getMessage() { return message; }
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.