0

So I have an application that communicates android devices in the same local network with each other. They periodically speak to each other by connecting to by establishing connection to serversocket, exchaning data and closing connection (every minute).

For some reason, it works well for first couple of attempts and then they start to return connectoin refused each time. Anybody can tell me what could be the reason?

My code is very simple:

server = new ServerSocket(0);
while(true) {
    Socket clientSocket = server.accept();
    Log.d(TAG, "Incoming connection");
    handleClient(clientSocket);
}

And when connecting:

    Socket s = new Socket(si.getInet4Addresses()[0], si.getPort());
    doStuff(s)

I have the following permissions:

    <manifest>
      ....
      <uses-permission android:name="android.permission.INTERNET"/>
      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
      <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
      <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
      <uses-permission android:name="android.permission.WAKE_LOCK"/>
      <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
      <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
      <application
        <activity>
          ...
          <intent-filter>
            ...
          </intent-filter>
        </activity>
        <service .. />
        <receiver >
           ....
        </receiver>
      </application>
    </manifest>

Oh and should I mention it all happens in a service.

Charles
  • 50,943
  • 13
  • 104
  • 142
siemanko
  • 1,389
  • 1
  • 13
  • 26
  • May i see the ``handleClient()`` definition? –  Jan 10 '13 at 00:36
  • Are you closing the sockets at the end? Maybe you are reaching the maximum number of opened sockets allowed? check this: http://stackoverflow.com/questions/1073203/java-net-connectexception-connection-refused-when-socketchannel-open-is-invoked and this http://stackoverflow.com/questions/2185834/maximum-number-of-socket-in-java – Adel Boutros Jan 10 '13 at 00:13
  • Yes, at some point I hoped it was it. But then I started closing all the sockets and still had no luck :( I am doing it in finally block, so I am pretty sure they are closed. But just for sanity check is there any way to check total number of openend sockets? – siemanko Jan 10 '13 at 00:14
  • I thuroughly checked my code flow, I am sure, I am closing all the sockets that are openend. – siemanko Jan 10 '13 at 00:24

0 Answers0