How to communicate without prior knowledge of IP - android

Im developing an app which uses the tcp connection. currently im communicating using hard coded IP addresses as a sample, but in the real world this is not the case i think. We come across any mobile and start communicating/sharing etc without having any prior knowledge of other person's IP. In such a case how to get the IP address of those who are using my application. How can i communicate without the prior knowledge of the IP address. How to implement this. Help me in sorting it. Thanks in advance.
EDIT:
And in case if the user connects the internet thro' GPRS/3G connection then his IP address will be changing based on the service provider. How can i find that.

Given what you are commenting on other answers, if what you want are the IP addresses of other Android devices to do some kind of P2P game you should note that there is no reliable way to do that directly.
If the users are connected in the same LAN you could provide in your application some kind of discovery service using UDP broadcasts.
If the users are connected to the internet and have public IP addresses then you could use some intermediate server to register the users at startup and have them discover other users using that server.
But (and this is the most common case) if the users are connected to the Internet and have private IP addresses (like when they are connected with WIFI on their LAN) you need some kind of server that acts as a proxy for their requests because there is no way to make a direct TCP connection from a natted IP to another natted IP. There are a few solutions to solve this problem, you could start for example by learning something about XMPP and how it works.

Your app must use DNS. You will first need get your IP into the DNS system. You do this by signing up for a domain name & setting up A / AAAA records for hostnames with a hosting provider (or you could set up your own DNS server). You may even find some free DNS providers.
In your app, you can hard-code a fully qualified domain name that you previously set up, say - app.foo.com and use the android gethostbyname library call to fetch the IP address for you. The local DNS resolver will then go to its DNS server and fetch the IP address corresponding to app.foo.com.

Can you use DNS? You can still hard-code the domain name if you want, and the domain name can be configured to point to any IP addresses anytime later.

Related

Local network ARP requests from Android app

I'm working on an app interacting with Network Enabled devices on a local network.
I'm currently working with IP addresses. However, on the long run, IP addresses will eventually change, breaking the connection. I'd like to store MAC addresses instead, and init my app by sending ARP requests and fetching the matching IP address for each stored MAC.
Is this the correct way to do this?
If so, which library should I use for such a task, and do any of you have a code example?
Many thanks!
You will probably need to access low-level components which Android & Java don't allow you to. Maybe with a rooted device ?

How to use Android_ID to communicate with other device?

My router shows
14:AA:BB:CC:DD:46 android-################ 192.168.3.112
AC:AA:BB:CC:FF:46 MY-LAPTOP 192.168.3.112
AC:AA:BB:CC:FF:AC NAS 192.168.3.112
Now if I use ftp://NAS/ it works, but
if I use ftp://android-################:7721/ it does not work, again ftp:/192.168.3.112:7721/ works, I don't understand what is the use of Android_ID then.
I thought since Android IP keeps varying on router we can find android by using Android_ID, but that's not the case.
Can anyone explain why is it happening?
Your Android device uses DHCP to get an IP address from the router on your network. While a router will try to use the same IP address across sessions, there's no guarantee you'll always be handed the same IP address. You generally do not want to make any assumptions about it not changing.
Assuming you have client software that's trying to talk to a service running on the Android device you need to implement some sort of network discovery mechanism. Android has a mechanism in the SDK that uses mDNS called "NSD". See https://developer.android.com/training/connect-devices-wirelessly/nsd. You could also use UDP broadcast.
"android-####" is a local hostname. Unless you have registered your Android device with a DNS server serving your local network you need to use IP addresses (and in that case you'd need to configure your router to also give your Android device the same IP address).

Finding unused IP addresses

I have tried searching for this, but nothing has met what I need so far. I am writing an FTP server application for Android, but I do not know how to find an IP address for the server that is not in use.
I have so far been guessing and hardcoding in an address. Sometimes it would work and sometimes it wouldn't. How can I get my app to consistently generate and connect to an open, usable IP address?
Thanks!
The standard solution to the problem is to obtain a DNS name (usually at a small expense), and then use a dynamic DNS service to keep telling the DNS hosting provider the current IP address for the DNS name.
Then clients can lookup your IP address from a well-known name.
There is also an indirect solution to the problem. You can get a infrastructure provider to host the FTP service. For example, you could run a free Amazon Web Services micro instance. Whilst it is running it should be on a constant IP address. Then both your clients and your app can use this as a fixed intermediate relaying point. This also solves the transient nature of mobile devices which might not be available to respond at a particular time.

Adding an android phon to a DNS server?

I want to know if it is possible and how to, make an application, that allows me to put my android phone in a DNS server, as we do in a Windows PC. I want to identify my android phone by a name in a DNS network, obviously in a network in which there is a DNS server.
Thanks in advance...
Generally, you have three options:
Static IP. Sometimes it's possible to get a fixed IP for your phone, in this case you need to setup a DNS record only once, and it requires no software nor configuration on the phone itself. All you need is just create a regular DNS address record. (Consult your DNS administrator (or ask on superuser.com if your need help))
Home/office network (DHCP). If you want to access your phone via LAN, and this LAN have a DHCP server under your control, DHCP server can be configured to update a DNS record whenever your phone connects. In this case too, the phone does not need any special configuration/software - it's a DHCP server which needs to be configured.
Dynamic DNS. Your phone can submit it's address via dynamic DNS. This will work for any case - LAN (including NAT) and cellular connection. You usually need to subscribe to some dynamic DNS service for this and yes, there are dynamic DNS clients for Android. Try net-me.net guide on setting up dynamic DNS under Android (I am affiliated).
(For what purpose you want access your phone, which software will listen to incoming connections, and if the phone IP will be accessible from anywhere - it's all a completely different story.)
If your DNS and DHCP servers support it, you can get the DHCP server to automatically update the DNS server. See http://technet.microsoft.com/en-us/library/cc787034(v=ws.10).aspx for a description of how to do it with Microsoft's servers.

Does Wifi access points have an ip address?

I am trying to make an application which can connect from a mobile to a wifi controller. Planning to do a socket connection(multicast) ... The only values that i am able to get through the wifi android apis are SSID and BSSID.. Can anyone tell me if i am supposed to proceed with these values or am i supposed to connect with an IP address ? If i am supposed to connect with BSSID.. how do i go about it?
Wifi access points need not have an IP address, any more than wired switches and hubs do. However a large number do, as they also act on the IP layer as DHCP servers and network gateways.
What exactly are you trying to do? It sounds likely that it is not the Wifi access point proper that you need to talk to.
If it supports TCP and UDP, it will of course have an IP address. This will not be directly discoverable from the Wifi APIs, and you'll need some other means of discovering that. It's hard to say what exactly will be a reasonable method to do this without knowing what the AP is, and what services it provides.
Common ways of enumerating services provided on a network include DHCP; multicast DNS/DNS service discovery which is used by "Bonjour" and "Zeroconf"), SSDP and uPnP, and of course static configuration.
I'm not familiar enough with the Android network APIs to recommend anything, though http://jmdns.sourceforge.net/ appears to be a usable Java multicast DNS library.

Categories

Resources