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.

public class Soc2 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;
        }
    } 
}

소켓 때문에 검색하다 찾은 소스 인데요.

에뮬이 아니라 실제 단말에서 서버소켓을 만들어서 연결 하려고 하는데.. 전 아무리 해도 안되더라구요.

위에 글 쓴 사람은 연결은 된 거 같은데 클라이언트쪽 코드는 없고 서버쪽만 있더라구요..

아래쪽은 제가 테스트 하는 클라이언트쪽 자바 소스 이에요. 어디가 잘 못되서 연결이 안되는건지..

public class deskTopClient implements Runnable
{
 private static final String TAG = "deskTopClient";
 private static final int READ_BUFFER = 1024;
 private static final String SERVERIP = "192.168.10.103";
 private static final int SERVERPORT = 5002;
 
    public void run() 
    { 
  String message = "Hello from Client";
  
   InetAddress serverAddr = InetAddress.getByName(SERVERIP);
   
          
   Socket socket = new Socket("192.168.10.103", SERVERPORT);
   System.out.println("Connected");
   
   try {
    // 메시지 전송
    PrintWriter out = new PrintWriter(new BufferedWriter(
      new OutputStreamWriter(socket.getOutputStream())), true);
    out.println(message);
    System.out.println(message);
    out.flush();
    
    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    while((message = br.readLine()) != null) 
    {
     System.out.println(message);
    }
   } catch (Exception e) {
    System.out.println(e);
   } finally {
    socket.close();
   }
   
  } catch (Exception e) {
   System.out.println(e);
  } 
  
  
    }
    public static void main(String a[]) {
  Thread desktopClientThread = new Thread(new deskTopClient());
  desktopClientThread.start();
 }
}