SeverSocket stops accepting

Go To StackoverFlow.com

0

I'm working on a game with a event based structure with the main game logic hosted on a server; currently it's a very small featureset that only allows to host one game between exactly two participants. I've read on various questions about ServerSocket and none of them answers my question. I already took a look at

In my project I utilize ObjectInputStream and ObjectOutputStream. Everything works as expected (receiving / sending both on server and client side), but after both sockets are registered, the accept method of the ServerSocket instance continues to block forever, even if the same code is invoked before. Perhaps it's an issue that appears after communicating over a socket once?

My server log shows the following:

waiting for accept
accepting first socket
sending an event to socket1 for informing about waiting for the opponent
waiting for accept
accept second socket
sending responses to both sockets
waiting for accept (and blocking forever)

When the log says response events where sent, they were properly received and processed at the client side. The client side debug outputs show that the next event is definitely sent. Maybe it's about not closing the client sockets (mentioned in the third linked question)? Anyway I can't close the client sockets because further communication would be impossible.

Client side code

public void send(Event e) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(
                socket.getOutputStream());
        out.writeObject(e);
        out.flush();
        log.debug("sending event... "+e);
    }
    catch(IOException ioe) {
        log.fatal("constructing oos failed", ioe);
    }
}

Server side code

@Override
public void run() {
    running = true;
    while(running) {
        try {
            Socket s = socket.accept();
            ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
            Event event = (Event) ois.readObject();

            try {
                Event[] response = controller.consume(event);
                ObjectOutputStream oos = new ObjectOutputStream(sockets[0].getOutputStream());
                oos.writeObject(response[0]);
                oos.flush();
                ObjectOutputStream oos2 = new ObjectOutputStream(sockets[1].getOutputStream());
                oos2.writeObject(response[1]);
                oos2.flush();
            }
            catch(...) {
                // multiple catch clauses for different exceptions
                // all just logging (nothing passes silently!)
            }
    }
}

For shortening, the method for assigning the two sockets to the Socket[] array was left out, but since there are no exceptions, keeping the socket works. Do you have any idea what could cause the described behavior? Thank you in advance.

2012-04-05 22:21
by phineas
I suggest you read the Networking tutorial. Especially the socket section - Jeffrey 2012-04-05 22:29
I've already read the tutorial. It doesn't cover objectstreams and four lines of pseudo code on handling multiple clients aren't enough - phineas 2012-04-05 22:38


1

The accept method only accepts new connections. Since you only have two clients attempting to connect to your server, it will hang indefinitely on your third invocation of accept.

Side note: You don't need to continuously create new ObjectInputStreams and ObjectOutputStreams. You can just create one of each for each Socket and keep references to them for reuse.

2012-04-05 22:51
by Jeffrey
Good hint. So structural aspects have to be changed. What would be your approach to handle both behavior, registering new connections and maintaining the existing ones - phineas 2012-04-05 23:00
Multiple threads, or use non-blocking IO - Jeffrey 2012-04-05 23:08
Ads