Monday, January 19, 2009

How to get IP of your mobile phone

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)"socket://:1234");

Note: This method may return your IP Address as (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


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 or or 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.