2

I have an application that listens on a port. I already acqired a WakeLock to assure, that the application also listens in standby mode. If the connectivity changes, the applications opens a new port to listen. But what is the best way to assure, that the application opens/logs in a new port, when the ip of the router changes? Is there maybe a package routers send, when they change their ip (to the internet) to notify their hosts? Or do I have to periodically check the ip of the router, if it has changed? If so, how do I retrieve the routers internet ip address?

Simon
  • 53
  • 5

1 Answers1

1

I don't believe there's a way to know a router device has changed its public IP, so you'll have to poll it by yourself. There are 2 things that come to my mind that you could do

  • Acquire a free DNS redirection. Most routers support some no-ip.com type configurations, so you enter your credentials here and each time it acquires or restarts it will login to your DNS redirection provider and update the IP to which it points. So within a minute you'll be able to ping (or connect to) your dynamic host and know the new IP. In that case, if you register simon.no-ip.com, you'll have always your current public direction here. You must check if your router supports that feature, though.

  • Poll some dynamic IP information website. There are lots of websites that retrieve your current public IP direction. You could simply open op that website from time to time and check for updates for your IP. An example website would be this.

nKn
  • 13,691
  • 9
  • 45
  • 62
  • Thanks for your answer. I may should have gone into more detail. The application I'm developing is a messenger. So using dynamic DNS ain't a solution. If the server receives a message, it forwards it to the port and ip address the distinct receiver has logged in. But if the router in front of the receiver changed the ip address since the last login, messages will not be received until next login. I would like to avoid periodically sending messages to my server. – Simon Apr 07 '14 at 12:41
  • Have you considered using `Google Cloud Messaging`? It does exactly what you describe and you get rid of any networking related problems, as Google already manages sending the messages to the recipients subscribed to your app. – nKn Apr 07 '14 at 13:04
  • Didn't know Google Cloud Messaging before. I'll get some information about that. But as far as I'm developing an encrypted messenger, that should be trusted, it is not a good idea to send data to Google. Even if it is only to get the current ip. Google still knows who uses the service and when they are logged in. I don't know yet, but I guess they do upload and save additional data of the users...But I'll give that a thought thanks :) – Simon Apr 07 '14 at 13:49
  • It's up to you if you want to do something by yourself or not, in any case if you want more info about it, there's an answer that might help you: http://stackoverflow.com/questions/22503106/how-to-send-location-of-the-device-on-server-when-needed/22566314#22566314 The secureness of third-party apps is a long-wide subject to be treated here, but consider the difficulties you'd face if you had to manage those IP addresses of your clients and also consider that most of your users already use some Google services (as far as I know those messages are not stored, at least not publicly). – nKn Apr 07 '14 at 13:53
  • Yes, you gave me some possibilities, now I have to decide which one to use. I think I will go with an existing ip query site as far as they allow to use them programmatically or periodically login to my server. Right now I'm facing the problem that my WakeLock seems to not lock. I constantly listen on a port. But when the screen is off for a while, it doesn't listen anymore. The Thread that locks, starts listening and then unlocks is called by another thread in a Service. – Simon Apr 07 '14 at 14:18