Prevent application hanging when server unavailable

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Prevent application hanging when server unavailable

Postby nikogamulin » Tue Mar 17, 2009 3:51 am

Hi,

I have created a multithreaded application but it still hangs if the server is unavailable.

In the main Activity I've created the following methods:

Code: Select all
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //bind GUI elements
        ...

        //start TCPConnection Service in a new thread
        establishTCPConnection();
        ...

    }

...

private void establishTCPConnection(){
       TCPClientServiceThread = new Thread(null, backgroundConnection, "connection");
       TCPClientServiceThread.start();
    }

..


private Runnable backgroundConnection = new Runnable(){
       public void run(){
          //establish TCP connection
          doEstablishTCPConnection();
       }
    };
   
    private void doEstablishTCPConnection()
    {
       //start TCPConnection service
       startService(new Intent(this, TCPClientService.class));
    }


And this is the TCPClientService Class:



Code: Select all
public class TCPClientService extends Service{
   
   ...
   
   private String serverAddress = "192.168.1.5";
   private int portNumber = 1000;
   
   
   
   @Override
   public void onCreate()
   {
      //TODO: Actions to perform when service is created
      connectionAvailable = false;
      
      IntentFilter dataReceiverFilter;
      dataReceiverFilter = new IntentFilter(MotranetClient.MOTION_DATA_UPDATED);
      dataReceiver = new DataReceiver();
      registerReceiver(dataReceiver, dataReceiverFilter);
      
      EstablishConnection();
   }
   
   @Override
   public IBinder onBind(Intent intent)
   {
      //TODO: Replace with service binding implementation
      return null;
   }
   
   private void EstablishConnection()
   {
      try {
              InetAddress serverAddr = InetAddress.getByName(serverAddress);
              Log.d("TCP", "C: Connecting...");
              Socket socket = new Socket(serverAddr, portNumber);
              String message = "testing connection";

                  try {
                   Log.d("TCP", "C: Sending: '" + message + "'");
                   PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
                   out.println(message);
                   Log.d("TCP", "C: Sent.");
                     Log.d("TCP", "C: Done.");
                     connectionAvailable = true;
                  
                } catch(Exception e) {
                    Log.e("TCP", "S: Error", e);
                    connectionAvailable = false;

                  } finally {
                     socket.close();
                     announceNetworkAvailability(connectionAvailable);
                   }

            } catch (Exception e) {
                 Log.e("TCP", "C: Error", e);
                 announceNetworkAvailability(connectionAvailable);
            }
       }
   
   
}



When the server is unavailable the line
Code: Select all
Socket socket = new Socket(serverAddr, portNumber);
causes some delay and I believe this is the cause for hanging. But if the TCPClientService Service is executing in its own thread I don't know why this affects the timeout of the main activity.

I would be very thankful if anyone could show how to prevent the application from hanging when the server is unavailable.

Thanks!

Niko
nikogamulin
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Mar 17, 2009 3:31 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests