Hey everyone,
I have a client/server set up. The server (Provider.java
) will accept 2 Socket connections. Each client, after they connect, are asked what type of game they would like to play - Tic Tac Toe, Chess, or Connect 5. Depending on their answer, the Socket connections are then stored in the particular game Queue. So, Tic Tac Toe has a Queue, and Connect 5 has a Queue, etc.
The Queues are implemented as follows:
Queue<Socket> qTTT = new LinkedList<Socket>();
Queue<Socket> qC5 = new LinkedList<Socket>();
Queue<Socket> qChess = new LinkedList<Socket>();
Each time a connection is added, the queues are checked to see if there are two connections. If there are two connections in a particular queue, then a game of that type will be launched, with the two connections. (designed with Factory design pattern).
if (qTTT.size() == 2) {
ObjectOutputStream out = null, out2 = null; // Client 1, Client 2
ObjectInputStream in = null, in2 = null; // Client 1, Client 2
Socket connection1 = null, connection2 = null;
connection1 = qTTT.remove();
connection2 = qTTT.remove();
try {
out = new ObjectOutputStream(connection1.getOutputStream());
out2 = new ObjectOutputStream(connection2.getOutputStream());
in = new ObjectInputStream(connection1.getInputStream());
in2 = new ObjectInputStream(connection2.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
SimpleGameFactory factory = new SimpleGameFactory();
GameStore store = new GameStore(factory);
Game game = store.orderGame("TTT");
System.out.println("We ordered a " + game.getName() + "\n");
tgame.setObject(game);
tgame.setSockets(connection1, connection2);
tgame.setStreams(in, out, in2, out2);
// Start the threaded game
tgame.start();
}
The above is throwing some sort of Stream corruption error. Here are my questions:
First, when the clients first connect, the socket connections are associated with an ObjectInputStream
and an ObjectOutputStream
. Those streams are not passed into the queue with the connections, so in the code above, new streams are assigned to the connections. Then, those streams are passed into the threaded game to start the game. Am I allowed to do this (assign new streams to the connections)? After streams had already been assigned to them?
Second, am I storing/removing the socket connections in the queue properly?
Thanks for your help!
UPDATE:
Here is the error I have been getting:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2232)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2698)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:750)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:268)
at Pools.check_pools(Pools.java:34)
at Provider.start(Provider.java:85)
at StartServer.main(StartServer.java:16)