开发者

Java Socket Connections in Queue

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 connectio开发者_StackOverflowns 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)

Turns out that I cannot assign an additional set of Input/Output streams to socket connections which have already been assigned a set of streams.

Thanks all for your help!


Honestly, that question looks like a solution to a college assignment.

Hint: write simple publisher/subscriber first. Set up one process that will be writing hello world to a socket and the second process that will be reading out of this socket. Then take it from there. I bet you're missing something obvious :-)


Are you sure that all of the opening handshake was retrieved from the socket before constructing the Object IO streams?

For example, if the client sends TTT\n\n as the game request and your code only reads the TTT\n and leaves the other \n on the stream, then the ObjectInputStream will fail.

You don't show the part where the user's game choice is read from the socket. So, this is just a guess.


My guess is that the exception has something to do with the code that you described at the beginning of your post. I'd start by looking into what you're doing with the sockets before your program gets to that snippet of code you posted.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜