How to get the IP Address of a mobile using Java ME?

There are many situations where you may want to know your phone's IP Address. You know that your mobile connects over GPRS which in turn is based on Internet Protocol, so you are pretty confident that the phone has an IP Address.
Well then, there is a good news and a bad news for you. The good news is that your phone do have an IP Address. The bad news is that this IP address will be hardly usefull for you. Why? read on.
The above image shows the network layout of most mobile based IP connection. Your mobile handset will be connected to the service provider's NAT gateway. The IP that your mobile handset get is a private IP, means that this IP can't be reached from external world.
There are ways to get both your private IP and the IP Address of the NAT gateway.
Getting your IP Address
1)The following snippet will give you your private IP Address.
ServerSocketConnection scn = (ServerSocketConnection)Connector.open("socket://:1234");
System.out.println(scn.getLocalAddress());
Note: This method may return your IP Address as 127.0.0.1 (the loopback address) if the GPRS link has been idle. So always send some data over the connection before testing this.
2)Using the System properties
System.getProperty("microedition.hostname");
This is supposed to give you the IP Address. But for me this returns 127.0.0.1 always. If you had any success with this, please share in the comments.
3)This is a bit round about method, but is the most guarenteed to work one. If you are trying to connect to your custom server, have the server tell your ip to you. Implement a handshake protocol, where you say "Hello server" and the server replies "Hello client. Your IP is xx.xx.xx.xx".
Getting the IPAddress of the NAT gateway
Service like http://whatismyipaddress.com/ or http://www.lawrencegoetz.com/programs/ipinfo/ or http://www.whatismyip.com/ gives the IP Address it sees when you browse their pages. This will be your NAT IP Address. Just parse the page and get the IP.
Remember all these works only if you are using GPRS/Wifi. If you want to know the IP address it need to be a IP based network.
If all you want is a unique number to identify your mobile, read this post on using IMEI number
Post your success stories and failure in the comments. Let me know how much this post helped you.


11 Responses to How to get the IP Address of a mobile using Java ME?
Good article.
This post has been removed by the author.
gr8 wrk..!! but first two dont seem to be wrkin...i didnt quite get the third one....how will the server know the ip address of the client??? i.e how can i extract that.....pls reply soon.... btw my mail id is sujay.bitspilani@gmail.com
bye
Very good article, I only wish I'd known that before
I've experienced the same thing with kleppner - the first two didn't work for me too. I wanna ask if the two code snippets would work on S40 or not..
And oh, what i mean by 'not working' is that they throw exceptions. The first one was a socket exception and the second was a null pointer exception.
Very good article. How do I get the IP of my blackberry?
Can anyone recommend the top performing Endpoint Security system for a small IT service company like mine? Does anyone use Kaseya.com or GFI.com? How do they compare to these guys I found recently: N-able N-central network security
? What is your best take in cost vs performance among those three? I need a good advice please... Thanks in advance!
hi , thank u great weblog
i try this but get NULL message :P
i have Samsung F480i . i try to open
a socket connection like this :
>
ServerSocketConnection serv = ServerSocketConnection)Connector.open("socket://:1234");
>
and then get the local address like this :
>
serv.getLocalAddress()
>
but problem is here :
every time i run the appliaction in this version of mobile local address has been changed :(
do you have any idea about it ?
A great place to start is http://www.reportipedia.com - it's a wiki with information on all the major reporting packages.
WOW!
Thanks a lot for sharing such a nice and informative article,Really a very nice and detailed review on it, which is very appreciable. i was trying to do all the day and your post had helped me a lot.
By the way for more latest java interview questions check this link: java interview questions