1

I am working on a university assignment, I have to lunch 3 servers that deal with client and a leader for the group of servers, here is the scenario:

  1. leader starts.
  2. leader runs 3 servers ( MUST run them as jar files using Runtime.getRuntime().exec();
  3. send activation message to the servers to start serving the clients
  4. client start to communicate with servers.

the problem is after 3 or 4 requests from the client the server hangs, and only complete its work if the the leader terminates. If I run the jar files manually from the cmd everything works fine, the problem only rise if the leader lunch the jar files through process.

here is some code - server main node, which accept any new connection, either from the leader of the group or from the clients to be served:

public void startServer () throws IOException{
        //new LogSetup ( "logs/server/server.log" , Level.ALL );
        this.serverStatus = ServerStatuses.UNDER_INITIALIZATION;
        running = initializeServer ();
        if ( serverSocket != null ) {
            while ( isRunning () ) {
                try {
                    Socket client = serverSocket.accept ();
                    ConnectionThread connection = new ConnectionThread (
                            client , this );
                    new Thread ( connection ).start ();

                    logger.info ( "new Connection: Connected to "
                            + client.getInetAddress ().getHostName ()
                            + " on port " + client.getPort () );
                } catch ( IOException e ) {
                    logger.error ( "Error! "
                            + "Unable to establish connection. \n" , e );
                }
            }
        }
        logger.info ( "Server stopped." );
    }

thread for new connection:

public void run () {
        try {
            output = clientSocket.getOutputStream ();
            input = clientSocket.getInputStream ();

            while ( isOpen ) {
                try {
                    AbstractMessage msg = receiveMessage ();
                    handleRequest ( msg ); // to determine the connection type
                    /*
                     * connection either terminated by the client or lost due to
                     * network problems
                     */
                } catch ( IOException ioe ) {
                    logger.error ( "Error! Connection lost!" +ioe.getStackTrace ());
                    isOpen = false;
                }
            }

        } catch ( IOException ioe ) {
            logger.error ( "Error! Connection could not be established!" , ioe );

        } finally {

            try {
                if ( clientSocket != null ) {
                    input.close ();
                    output.close ();
                    clientSocket.close ();
                }
            } catch ( IOException ioe ) {
                logger.error ( "Error! Unable to tear down connection!" , ioe );
            }
        }
        logger.info ( "exit the thread" );
    }
private void handleRequest ( AbstractMessage msg ) throws IOException {
        logger.info ( "inside handling abstract message" );
        if ( msg.getMessageType ().equals ( MessageType.CLIENT_MESSAGE ) ) {
            handleClientRequest ( ( ClientMessage ) msg );
        } else if ( msg.getMessageType ().equals ( MessageType.SERVER_MESSAGE ) ) {
            handleServerRequest ( ( ServerMessage ) msg );
        } else if ( msg.getMessageType ().equals ( MessageType.ECS_MESSAGE ) ) {
            handleECSRequest ( ( ECSMessage ) msg );
        }
        logger.info ( "finish handling " );

    }
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Bob Zant
  • 215
  • 4
  • 16

1 Answers1

1

The Leader process has to process the input/output/error from your servers. When a server loggs something, this is written to System.out (thats the default behaviour of most log frameworks). Since your leader startet the server process the output is written to a pipe. If the pipe buffer is full, the log blocks and your server 'hangs'.

Jens Baitinger
  • 2,230
  • 14
  • 34
  • I am sorry but I did not get it, any good tutorials about handling input/output/error ? – Bob Zant Dec 20 '13 at 15:58
  • Here is a question with the same problem: http://stackoverflow.com/questions/11336157/running-external-program-with-redirected-stdin-and-stdout-from-java – Jens Baitinger Dec 20 '13 at 16:00