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.