Search code examples
javaandroidudpbroadcastmulticast

How to communicate between two process and two hosts using UDP datagrams on Android


My distributed application, with various hosts need to find local servers within the same LAN.

I had limited success detecting local servers using NsdManager, which is the way to go, but often, servers remain undetected. I am looking for a better solution, something more reliable.

Then I attempted to multicast UDP packets, which I have no problem detecting from within the same process. (My unit test uses both client and servers instances). That works fine. The same classes will not allow my clients to detect my servers running on the same device (client and servers are separate process applications).

Then I varied my approach using broadcast addresses, again, within the same process, unit tests are fine, but in two distinct process, no server found.

As I said, the code works fine within the same process, but they do not work when running in two distinct processes, or on two distinct client / server hosts within the same LAN.

Note: my LAN is a high end WiFi rooter, no special settings.

Anyone has any clue what might be the problem?

Note that my manifest includes these settings for both client and server applications:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

Note that for my client to detect the server running on the same Host, I am already using TCP sockets, but I need to detect whatever server is in my LAN.


Solution

  • UDP communication cannot depend on any kind of processes. The problem is with your code.