4

我已经实现了自己的android服务如下

public class MyService extends Service {
    private static final String TAG = "MyService";

    private Server mServer; 
    private LocalServerSocket server;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        mServer = new Server();
        mServer.start();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        if(server != null){
            try {
                server.close();
            } catch (IOException e) {
                Log.d(TAG, "exception in server close");
                e.printStackTrace();
            }
        }
    }

    @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStart");
        return START_STICKY;
    }

    class Server extends Thread {
    @Override
        public void run() {
             try {
                server = new LocalServerSocket("my.socket");
                while (true) {
                    LocalSocket receiver;
                    try{
                        receiver = server.accept();
                    }catch(SocketException e){
                        Log.d(TAG, "SocketException");
                        break;
                    }
                    catch(IOException e){
                        Log.d(TAG, "IOException");
                        break;
                    }
                    if (receiver != null) {
                        Log.d(TAG, "Got Data in receiver");
                    }
                    receiver.close();
                }
            } catch (IOException e) {
                Log.d(TAG, "one more");
                e.printStackTrace();
            }
        }
    }
}

我面临的问题是,如果我的 LocalServerSocket 在 accept() 中阻塞,那么在 OnDestroy() 中对 server.close() 的调用将不会引发 SocketException。因此,下次我启动服务时,我得到“地址已在使用异常”。如果我使用 java.net.Socket 而不是 LocalServerSocket,那么我会得到所需的行为。我想知道为什么 LocalServerSocket 的行为与 Java 套接字不同。就我而言,我如何摆脱 while 循环。

4

2 回答 2

2

我遇到了同样的问题并以这种方式“解决”了它。线程 run() 方法正在检查“!isInterrupted()”。我添加到我的 Listener-Thread 的方法“stopSocketServer()”将线程标记为 interrupt(),然后向自身发出连接请求以触发 accept() 方法。

/**
 * Executed if thread is started.
 */
public void run() {

    try {
        // leave while loop if thread is marked for interrupt.
        while (!isInterrupted()) { 
            LocalSocket clientSocket = serverSocket.accept();

            if (!isInterrupted()) {
                threadPool.execute(new ClientProcessor(clientSocket));
            }
        } 

    } catch (IOException e) {
        if (!isInterrupted()) {
            Log.e(TAG, "socket listener terminated", e);
        }
    } finally {
        try {
            if (serverSocket != null) {
                serverSocket.close();
            }
            if (threadPool != null) {
                threadPool.shutdownNow();
            }
            Log.i(TAG, "socket listener stopped");
        } catch (IOException e) {
        }
    }
} 

public void stopSocketServer() {
    if (serverSocket != null) {
        try {
            // mark thread as interrupted
            interrupt();

            // now send connect request to myself to trigger leaving accept()
            LocalSocket ls = new LocalSocket();
            ls.connect(serverSocket.getLocalSocketAddress());
            ls.close();
        } catch (IOException e) {
            Log.e(TAG, "stopSocketServer failed", e);
        }
    }
}
于 2013-07-17T04:57:14.147 回答
0

从您发布的代码看来,您LocalServerSocket servernull永远存在并且onDestroy()不会关闭它。此外,关闭套接字通常不应该抛出 IOException - 但accept()如果套接字同时关闭,则会这样做。

于 2011-11-04T13:16:59.310 回答