Dear all,

I create a server to listen request from peer.
When client socket is connected, it will be leak after close socket.
The fd will increase and finally the process may be killed due to too
many fd.

Does anyone know how to safely close the socket?

The sample code is below.
=========================================================================
package com.android.test.serversocket;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ServerSocketChannel mainSocket = null;
        InetAddress serverAddress = null;
        try {
                mainSocket = ServerSocketChannel.open();
                mainSocket.configureBlocking(false);
                mainSocket.socket().setReuseAddress(true);
                mainSocket.socket().setSoTimeout(1000);
                String wifiIp = getWifiIpAsString();
                serverAddress = InetAddress.getByName(wifiIp);
                mainSocket.socket().bind(new InetSocketAddress
(serverAddress, 5002));
        } catch (IOException e) {
                e.printStackTrace();
        }

        while(mainSocket != null) {
            SocketChannel clientSocket;
            try {
                while((clientSocket = mainSocket.accept()) != null) {
                        clientSocket.close();
                        clientSocket = null;
                        System.gc();
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
            try {
                System.gc();
                Thread.sleep(1000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public int getWifiIpAsInt() {
        Context myContext = getApplicationContext();
        if(myContext == null) {
            throw new NullPointerException("Global context is null");
        }
        WifiManager wifiMgr = (WifiManager)myContext
                                .getSystemService
(Context.WIFI_SERVICE);
        if(isWifiEnabled()) {
            return wifiMgr.getConnectionInfo().getIpAddress();
        } else {
            return 0;
        }
    }

    public boolean isWifiEnabled() {
        Context myContext = getApplicationContext();
        if(myContext == null) {
            throw new NullPointerException("Global context is null");
        }
        WifiManager wifiMgr = (WifiManager)myContext
                                .getSystemService
(Context.WIFI_SERVICE);
        if(wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) {
            return true;
        } else {
            return false;
        }
    }

    public String getWifiIpAsString() {
        int addr = getWifiIpAsInt();
        if(addr != 0) {
            StringBuffer buf = new StringBuffer();
            buf.append(addr & 0xff).append('.').
            append((addr >>>= 8) & 0xff).append('.').
            append((addr >>>= 8) & 0xff).append('.').
            append((addr >>>= 8) & 0xff);
            return buf.toString();
        } else {
            return null;
        }
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to