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.
Related
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 ?
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).
Disclaimer: I don't know this is the actual problem, I just suspect it is.
I have set up an Amazon Web Services EC2 instance as a remote git server. I have had to configure it to only accept incoming traffic from a single IP address though I had wanted to make it open to all because I will be using it while I travel.
The complication here is that mobile phones cannot have a static IP address and I will be accessing the server using my android phone tethered to my laptop. This means I have to make a new rule to accept traffic from my current IP address before connecting to the server via ssh.
When setting this rule there is an option to use "My IP Address". Amazon automagically checks my current IP address and sets it in the rule. It looks like 123.123.123.123/32. The "/32" part there being the actual end of the address.
When testing the server from home I have no problem accessing the server from my desktop computer over the home network with static IP. Equally no problem accessing the server from my laptop computer over the home network. I then tethered my phone to my laptop and tried to access the server via the cellular network with the home static IP address rule still in place. As expected the connection was not made because the phone is allocated a different IP address.
BUT...then I go out for a day of coding in a nice location and try to connect to the server and it does not work. I went in to my EC2 console and set a new rule to accept traffic from my current IP address over my tethered phone using the automagic "My IP Address" setting. Then tried to connect to the server via ssh. No joy.
The connection is not made. Just times out in the same way it did when I originally tested trying to access from an IP address not specified in the rule.
So I was thinking it has something to do with ports. Is that what the "/32" above is? Or have I got that all out of whack?
What can I do to gather more information to help define what the actual problem here is?
I had this same problem connecting to a Postgres database in AWS. Here is what I did to solve it:
I opened up the server to everyone 0.0.0.0/0
I went into the server and determined my actual ip address that it was trying to connet with.
Went back and fixed my rule with that IP address.
My automatically populated IP address was different than the actual one.
Find an Android VPN provider that will sell you a static IP for every use. That might be a way to use a static IP as a security measure. If the VPN provider offers the service on PC as well you can set both to the same IP. Your really ambitious you could set up a VPN service on your virtual machine and then configure it to only except connections from it's self via SSH. You would have to Adjust your "security group" to except VPS connects from any where. The second method would mean that you wouldn't need two static addresses for and added layer of security.
basically, I want my android app to talk to a server; however, the server will change ip address - how do I get the app to pick up on this and adjust the ip address its sending to accordingly?
better wording: I am running a server on a local network, whose IP may change. How can I have my Android app detect the server and connect to it?
NOTE: I can't use DNS because the DNS may not update for 24 hours, and I need it to be updated instantly
As Paul said in the comments, you need DNS.
You could try No-IP, it is free.
Set a static IP on the server or setup a DNS record. DynDNS is a personal favorite.
I'd personally avoid the static IP route but it's OK for testing ...
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.