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:
- leader starts.
- leader runs 3 servers ( MUST run them as jar files using Runtime.getRuntime().exec();
- send activation message to the servers to start serving the clients
- 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 " );
}