I am trying to create a monitor of messages between two applications. The idea is this monitor works in the middle of simple client/server application, and log the messages to the standard output. This program must be against of fails of the client/server (disconnections, time out's, etc). In the code, i call the client as "origin" and the server as "destiny". The current problem is when the server dies my program doesn't know and when a new message from client comes, this error appears "Software caused connection abort: socket write error". When the server comes up to life again, this error continues persisting. I think when i ask in the code "if ( !socketToDestiny.isConnected() )", it is not really connected. I am sure the problem is how i manage the "close" at the stream too.
This is the code of the program, i hope you could help me.
package interceptorprocess;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
public class GenericInterceptorProcess implements Runnable
{
private final String prefix_log_messages = "[CONNECTOR]";
//COMMUNICATION'S ORIGIN'S VARIABLES
ServerSocket serverSocketLocal;
Socket socketForLocal;
DataInputStream streamFromOrigin;
DataOutputStream streamToOrigen;
int len_message_from_origen;
byte[] buffer_msg_origin = new byte[4096];
byte[] message_origin = null;
//COMMUNICATION'S DESTINY'S VARIABLES
Socket socketToDestiny;
DataInputStream streamFromDestiny;
DataOutputStream streamToDestiny;
int len_message_from_destiny;
byte[] buffer_msg_destiny = new byte[4096];
byte[] message_destiny;
@Override
public void run() 
{
    //OCCASIONAL USE
    String aux;
    try
    {
        logger("STARTING SERVER --- PORT NUMBER: " + "1234");
        //CREATING THE LOCAL SERVER SOCKET
        serverSocketLocal = new ServerSocket(1234);
        //CREATING THE DESTINITY CONNECTION WITH 15 TIMEOUT'S SECONDS
        socketToDestiny = new Socket();
        socketToDestiny.setSoTimeout(15000);
        //THIS LOOP MAINTAINS THE CONNECTIVITY WITH ONE CLIENT AT TIME
        while ( true )
        {
            logger("WAITING FOR A CONNECTION OF A CLIENT...");
            socketForLocal = serverSocketLocal.accept();
            //CREATING THE ORIGIN'S STREAMS
            streamFromOrigin = new DataInputStream(socketForLocal.getInputStream());
            streamToOrigen = new DataOutputStream(socketForLocal.getOutputStream());
            logger("CONNECTED CLIENT: " + socketForLocal.getRemoteSocketAddress() );
            //THIS LOOP MAINTAINS THE MESSAGES'S CHANGES
            while ( true )
            {
                logger("WAITING FOR A MESSAGE..");
                len_message_from_origen = streamFromOrigin.read(buffer_msg_origin);
                if ( len_message_from_origen < 0 )
                {
                    closeOriginStream();
                    break;
                }
                message_origin = new byte[len_message_from_origen];
                //SAVE THE ORIGIN'S MESSAGE INTO AN ARRAY WHO HAS THE EXACT SIZE OF THIS MESSAGE
                System.arraycopy(buffer_msg_origin, 0, message_origin, 0, len_message_from_origen);
                aux = new String(message_origin);
                logger("RECEIVED MESSAGE FROM ORIGIN: " + aux);
                //TRY TO CONNECT TO DESTINY
                try
                {
                    if ( !socketToDestiny.isConnected() )
                        socketToDestiny.connect(new InetSocketAddress("10.10.200.200",1234),5000);
                }
                catch(IOException ex)
                {
                    logger("CONNECTION REJECTED BY DESTINY: " + ex.getMessage());
                    continue;
                }
                //CREATING THE DESTINY'S STREAMS
                streamFromDestiny = new DataInputStream(socketToDestiny.getInputStream());
                streamToDestiny = new DataOutputStream(socketToDestiny.getOutputStream());
                logger("SENDING MESSAGE TO DESTINY: " + aux);
                //I HAD TO PUT THIS BLOCK BECAUSE IF THE DESTINY APPLICATIONS FAILS
                //OR NOT ANSWER, THE PROGRAM MUST KEEP LISTENING THE FOLLOWING MESSAGES
                try
                {
                    //SENDING MESSAGE TO DESTINY
                    streamToDestiny.write(message_origin);
                    //READING THE ANSWER MESSAGE
                    logger("READING MESSAGE FROM DESTINY...");
                    len_message_from_destiny = streamFromDestiny.read(buffer_msg_destiny);
                }
                //IN ONE OF THE FOLLOWINGS TWO CATCHS I GET THE ERROR 
                catch (SocketTimeoutException ex)
                {
                    logger("IT DIDN'T COULD RETRIEVE A MESSAGE FROM DESTINY: " + ex.getMessage());
                    continue;
                }
                catch (SocketException ex)
                {
                    //THE "socketToDestiny.isConnected()" ALWAYS RETURNS TRUE SINCE THE FIRST SUCCESSFULLY 
                    //CONNECTION, AFTER THAT, IF THE SOCKET IS DISCONNECTED, IT REMAINS RETURNING "true".
                    //THUS, I HAD TO MAKE THE NEXT CODE BLOCK
                    streamFromDestiny.close();
                    streamToDestiny.close();
                    socketToDestiny.close();
                    socketToDestiny = new Socket();
                    socketToDestiny.setSoTimeout(confs.timeout_destiny);
                    socketToDestiny.connect(new InetSocketAddress(confs.destiny_ip,confs.destiny_port),confs.timeout_connections);
                    streamFromDestiny = new DataInputStream(socketToDestiny.getInputStream());
                    streamToDestiny = new DataOutputStream(socketToDestiny.getOutputStream());
                    logger("TRYING TO RECONNECT WITH DESTINY AND SEND THE MESSAGE... ");
                    logger("READING MESSAGE FROM DESTINY AFTER ERROR...");
                    len_message_from_destiny = streamFromDestiny.read(buffer_msg_destiny);
                }
                message_destiny = new byte[len_message_from_destiny];
                //SAVE THE DESTINY'S MESSAGE INTO AN ARRAY WHO HAS THE EXACT SIZE OF THIS MESSAGE
                System.arraycopy(buffer_msg_destiny, 0, message_destiny, 0, len_message_from_destiny);
                aux = new String(message_destiny);
                logger("RECEIVED MESSAGE FROM DESTINY " + aux);
                //SENDING THE ANSWER BACK TO THE ORIGIN
                logger("SENDING BACK THE MESSAGE TO ORIGIN...");
                streamToOrigen.write(message_destiny);
                logger("MESSAGE DELIVERED SUCCESSFULLY!");
            } //INTERNAL LOOP OF MESSAGES
        } //INTERNAL LOOP OF CLIENTS
    } //TRY
    catch(IOException ex ) 
    {
        logger("THE SERVICE DIED: " +  ex.getMessage() );
        ex.printStackTrace();
    } //CATCH
} //RUN
private void closeDestinyStream() throws IOException
{
    streamFromDestiny.close();
    streamToDestiny.close();
}
private void closeOriginStream() throws IOException
{
    streamFromOrigin.close();
    streamToOrigen.close();
}
private void closeAll() throws IOException
{
    closeDestinyStream();
    closeOriginStream();
}
private void logger(String message)
{
    System.out.println(Utilidades.date() + " " + prefix_log_messages + " " + message);
}
}
Regards!
Sorry for my english, i am not a native speaker.
 
     
    