troubleshooting Question

Socket Connection in the presence of network faults

Avatar of java63
java63 asked on
Java
12 Comments1 Solution239 ViewsLast Modified:
Hi there

My task is to deal with network crashes using socket connection. I am simulating a transfer funds between two accounts that are modelled as simple integer.  This is only console based application. The possible problems are:

a). Process A cannot establish a connection with process B. The system display message cannot locate server.(Done)

b). Process A send request to process B. If timer expires before acknowledgement is sent back. Message is re-sent again, max 4 times and than go back to point a. (would be nice to have a loop going 4 times over – don’t know how to do it). I can manually reconnect a few times but it would be better to have a loop.

c.) (Most difficult to do it) Process A sent a request to process B. B receives it, execute it ( or only receives no execute). Timer is out (which indicate server has crashed). No replay sends for process A. We don’t want to transfer funds twice.

Possible solutions for c:
A send a request with attached additional bit 0 in the message header to indicate original request or 1 indicate re-transmission request.
B logs a request.
If B crashes it then check a previous status against the current one (compare added bits).
If status is 0 it means that it is original request, if status is 1 this is re-transmission request.
Process B will refuse to carry out any request a second time (to avoid mistake)


I think the server should save each request to file so it could then compare it with retransmitted one.  
My application can pass serialized object as an Integer from one machine to another and back. I know how to save serialized object using I/O streams in a single process and read it back. What I don’t know is to how to combine these two so I can send a request and then save it to file at the same time. Maybe you have different suggestions.
This is my code.


import java.io.*;
import java.net.*;

public class AccountServer extends Thread {
       private ServerSocket dateServer;

      /**
       * @param args
       */
      public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
             new AccountServer();

      }
      public AccountServer() throws Exception {
           dateServer = new ServerSocket(4000);
           System.out.println("Server listening on port 4000.");
           this.start();
         }

         public void run() {
           while(true) {
             try {
              System.out.println("Waiting for connections.");
              Socket client = dateServer.accept();
              System.out.println("Accepted a connection from: "+
      client.getInetAddress());
              Connect c = new Connect(client);
             } catch(Exception e) {}
           }
         }
      }

      class Connect extends Thread {
         private Socket client = null;
         private ObjectInputStream ois = null;
         private ObjectOutputStream oos = null;
          
         public Connect() {}

         public Connect(Socket clientSocket) {
           client = clientSocket;
           try {
            ois = new ObjectInputStream(client.getInputStream());
            oos = new ObjectOutputStream(client.getOutputStream());
           } catch(Exception e1) {
               try {
                  client.close();
               }catch(Exception e) {
                 System.out.println(e.getMessage());
               }
               return;
           }
           this.start();
         }

        
         public void run() {
               
               UserInfo user1 = null;
               
               try {
                    
                       user1 = (UserInfo) ois.readObject();
                       int amount = user1.getAmount();
                     
                     
                     // ship the object to the client
                     UserInfo user = new UserInfo(amount);
                     user.setAmount(amount);
                     oos.writeObject(user);
                     oos.flush();
                     // close connections
                     ois.close();
                     oos.close();
                     client.close();
                  } catch(Exception e) {}      
               }
            }

----------------------------------------------------------------------------------------------------

import java.io.*;
import java.net.*;

public class AccountClient {
   public static void main(String argv[]) {
      ObjectOutputStream oos = null;
      ObjectInputStream ois = null;
     
     
      // int value
      int amount = 100;
     
      try {
        // open a socket connection
        Socket socket = new Socket("localhost", 4000);
       
        // open I/O streams for objects
        oos = new ObjectOutputStream(socket.getOutputStream());
        ois = new ObjectInputStream(socket.getInputStream());
       
        // create serialized object
        UserInfo user1 = new UserInfo(100);
       
        // write the objects to the server
        oos.writeObject(user1);
        oos.flush();
       
        // read an object from the server
        user1 = (UserInfo) ois.readObject();
        System.out.print("The transfer is completed. The amount of £"+amount+" was received");
       
       
        oos.close();
        ois.close();
      } catch(Exception e) {
        System.out.println(e.getMessage());
      }
   }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros