开发者

Java ObjectInputStream hangs on second client instance

FOUND SOLUTION (consider this closed and answered since I can't answer my own question before 7 hours). I had left an unnecessary Socket instanciation and it made the clien apps hang. Sorry for bothering all of you for this and thanks for the effort put in helping me run around searching for the issue =) Love you all!

I am getting nuts finding the solution to this "problem". I have an bank simulation application that needs to simulate bank transfers between multiple branches of a bank. I currently use an event bus that dispatches different events objects that are caught by the clients' connector object. If the client listens to the event received, it will call a command object and update whatever it needs to update and so on (just so you get a big picture).

Now, for some reason I cannot understand, my first client instance opens fine, and behaves just as expected, but any client instances opened after the first one will hang in these lines (variables are declared prior to being initialized):

try {
        s = new Socket(ip, port);
        System.out.println("Creating out stream");

        oos = new ObjectOutputStream(s.getOutputStream());
        oos.flush();
        System.out.println("Flushed data...");

        ois = new ObjectInputStream(s.getInputStream());

        //Below never gets printed on second client instance *sigh*
        System.out.println("HURRAY");
        readStream = new ReadEventFromStream(ois, this);
    } catch(IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Can't connect to server.");
        System.exit(1);
    }

There is no exception thrown, no nothing...the second, third, ... instances launched will just never get past these quite simple lines. Just so you know, the above code is called in the EventBusConnector(...) object instanciated by the client main's function. Here is the ReadEventFromStream class making use of the command objects as stated before:

class ReadEventFromStream extends Thread {
private ObjectInputStream ois;
private EventBusConnector eventBusConn;
public ReadEventFromStream(ObjectInputStream ois, EventBusConnector eventBusConn) {
    this.ois = ois;
    this.eventBusConn = eventBusConn;
}

@Override
public void run() {
    while(true) {
        try {
            Object o = ois.readObject();
            if (eventBusConn.listensToEvent(o)) {
                Command command;
                if( o instanceof EventBranchListUpdate ) {
                    EventBranchListUpdate event = (EventBranchListUpdate) o;
                    command = new UpdateBranchListCommand(event, EventBusConnector.branch);
                    command.execute();
                } else if ( o instanceof EventNewBranch ) {
                    EventNewBranch event = (EventNewBranch) o;
                    command = new NewBranchCommand(event, this.eventBusConn);
                    command.execute();
       开发者_如何学运维         } else if( o instanceof EventMoneyTransfer ) {
                    EventMoneyTransfer event = (EventMoneyTransfer) o;
                    command = new MoneyTransferCommand(event, EventBusConnector.branch);
                    command.execute();
                }
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

}

So humm...yeah...I've been smashing my heading on this for hours and I really need some help because I'm getting nuts with this. If you need more code I will paste it. Thanks =)


Where are you creating a Thread for each connection?

Unless you create a thread for each connection, a new connection will not do anything until the last one finishes.


Oh well...after 8 hours of hard debugging, I found that the problem was my little self. I had left an unnecessary Socket instanciation roaming somewhere in the hidden corners of a class called by the client application...which made it hang. Sorry for bothering you all with this, I hesitated 8 hours to post it because I knew this was a complex application to debug when one is not writing the code...and I'm finding that thg

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜