Reputation: 45
I'm making an application which needs to communicate to another app on another device. The only problem is that the IP addresses from the devices aren't allways the same. I want to client to find the server on a specific port, but how can I find devices on the network which have this port opened without me having to enter the server's ip on the client side? I've found Android's NsdManager, but that works from API level 16 and on. I'm developing with level 10.
Thanks in advance!
Upvotes: 2
Views: 363
Reputation: 86
Broadcasting works in most cases, the link below shows how to do it from code: [Send Broadcast UDP but not receive it on other Android devices
But there are exceptions:
Some phones doesn't recevie broadcasting package correcttly(HTC for example), i solved this problem by broadcasting from HTC phone and once other phone recevied the package, send a udp package to HTC phone(not through broadcasting)
If one of the device is act as hotspot, the broadcasting seems doesn't work at all, in this case, other devcie can try use the gateway ip(which is the hotsport device's ip)
Upvotes: 0
Reputation: 45
My apologies for not responding.
I solved this problem by broadcasting an UDP packet to all devices (255.255.255.255). Al devices listening in the specified port will respond and thereby the client will now know the server's IP address. TCP is used for further communication.
Upvotes: 1
Reputation: 1009
You can use Zero-configuration networking which can help to solve your problem.Have a look at following websites-http://www.multicastdns.org/ and http://en.wikipedia.org/wiki/Zero-configuration_networking.
You can also make use MDSND, check Bonjour implementation on Android for more info.
Upvotes: 1
Reputation: 9375
This shouldn't be too difficult.
The private ip address in question should be of the form 192.168.1.x
(x being between 1 and 255)
And normally, I believe that number is assigned sequentially, so first I would check if
192.168.1.1
if not that one,
then I'd check
192.168.1.2
then
192.168.1.3
then
etc.
Upvotes: 0