Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close socket when finishing application

My application have some activities which in the first one I connect a socket to communicate with a server on the others activities. This socket runs in a worker thread.

My question is: where can I close this socket when the application is finishing? Using the BACK button for instance...

I thought close the socket in onDestroy() of the last activity but this activity can be destroyed by the system at runtime and close the socket even if the application is not finishing. I don't want this.

My run() method of the thread handling the socket connection is like:

public void run() { 
    if (this.bliveclient.isConnected()){       
        try {
            //...
            while (running) {
                //waiting for input data and do something...
            }
        }
        catch (IOException ex) {
            //handle exception
        }
        finally{
            try {
                mySocket.close();
            } catch (IOException ex) {
                //handle exception
            }
        }
    }

But the finally block is never called.

Can anyone give me some hint?

like image 643
amp Avatar asked Apr 04 '12 22:04

amp


People also ask

What does it mean to close sockets in WSK?

Closing a Socket. When a Winsock Kernel (WSK) application has finished using a socket, it should close the socket and free up any associated resources. A WSK application must close all open sockets before the application can detach itself from the WSK subsystem.

Does socketsocket close after the application exits?

Socket doesn't close after application exits if a launched process is open Ask Question Asked9 years, 9 months ago Active9 years, 9 months ago Viewed5k times 7 3 My C# .net application currently does the following (among other things):

When does an external process kill a socket?

It should be killing the socket when it exits, however when the external process is running that socket never closes. Any ideas? c#.netsockets Share Follow asked Apr 19 '12 at 19:09

How to close a connection in WebSockets?

WebSockets - Closing a Connection. Close event marks the end of a communication between the server and the client. Closing a connection is possible with the help of onclose event. After marking the end of communication with the help of onclose event, no messages can be further transferred between the server and the client.


1 Answers

The finally block is never executed because the run loop never completes. At some point Android just kills the process, which kills the VM and everything just goes away.

If you have an application that just contains activities and you are doing network I/O in a separate thread then there is no way for you to know when to shut the thread down because the "application" is never "finished". You need to define how the application "finishes".

A better way is to put this into a Service, because if Android wants to kill the process it will first call onDestroy() in the Service which will give you a chance to shutdown your thread properly.

like image 155
David Wasser Avatar answered Oct 03 '22 23:10

David Wasser