1

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 =)

2
  • try read first and then write Commented Jun 27, 2011 at 14:52
  • It still hangs; first client application runs fine like before and the second will never return from it's trip to the ObjectInputStream's constructor. Commented Jun 27, 2011 at 15:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

The EventBusConnector class extends Thread itself. It's run method is the following: while(true) { try { Thread.sleep(500); synchronized(lstEventsToSend) { if(lstEventsToSend.size() > 0) { IEvent ie = lstEventsToSend.get(0); System.out.println("Envoie de l'événement " + ie.toString()); oos.reset(); oos.writeObject(ie); lstEventsToSend.remove(0); } } } catch(Exception e) { e.printStackTrace(); } }
Yikes sorry I'll use pastebin I thought it would come out better.
And you will find here the client's main method and the Thread it calls to call an event stating that a new branch has logged on the system: pastebin.com/F5MdqawS
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.