开发者

How to communicate without prior knowledge of IP

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 p开发者_StackOverflow社区erson'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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜