Local Ip Address

196 views
Skip to first unread message

Mattia Parise

unread,
Jan 27, 2012, 11:02:00 AM1/27/12
to Cinterion Java enabled chips support, m.pa...@geckos.it
Hi to all.
I am trying to get the local IP address of my chip, but I think that
the ip that I get using a serversocketconnection ( with the method
geLocalAddress() ) is natted. However, the Method
System.getProperty("microedition.hostname") returns everytime null.
Is there a way to get that Ip?
Thanks in advance.
Best regards,

Mattia

em

unread,
Jan 27, 2012, 11:11:34 AM1/27/12
to java...@googlegroups.com, m.pa...@geckos.it
Hi,

geLocalAddress() is correct method
you're right the IP will be natted if you don't have pdp profile with
public ip associated.
You could use service like http://checkmyip.com/ but don't see any
reason to do it. Your device is not accessible from outside the
network anyway ;/

If you don't have public ip then your device has to initiate the
connection. The alternative solution is to use vpn.

KR makuda

> --
> You received this message because you are subscribed to the Google Groups "Cinterion Java enabled chips support" group.
> To post to this group, send email to java...@googlegroups.com.
> To unsubscribe from this group, send email to javacint+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/javacint?hl=en.
>

Mattia Parise

unread,
Jan 27, 2012, 11:24:29 AM1/27/12
to Cinterion Java enabled chips support
Em,thanks for you reply.
And if I use a service called Dyndns on the chip?Could it work? I have
some code to get the chip be the dyndns updater.
My only question is...will it work or not?
And does this page ( at the 3rd point) tells the truth about the ip
address? http://www.javameblog.com/2007/12/how-to-get-ip-address-of-mobile-using.html
Thanks in advance.
Mattia

On 27 Gen, 17:11, em <mak...@gmail.com> wrote:
> Hi,
>
> geLocalAddress() is correct method
> you're right the IP will be natted if you don't have pdp profile with
> public ip associated.
> You could use service likehttp://checkmyip.com/but don't see any

Nikita Kapitonov

unread,
Jan 29, 2012, 11:05:54 PM1/29/12
to java...@googlegroups.com
Mattia,
Dyndns will help you only if you have public dynamic IP. It solves problem of dynamic vs static IP, but still you need public IP.

Makuda is right, in most cases you don't have public IP, so you need to have the connection be initiated from mobile side.

If you want some actions to be initiated from user (server) side, you should consider some kind of persistent connection (see Florent's M2MP protocol) to achieve like real-time speed of communication, or some kind of way to "leave a data message" on server, to be transerred to mobile side on next communication attempt, as I do it in my current project (it is very easy realized on plain HTTP/PHP, no need to run other servers).

Best regards,
Nikita

2012/1/27 Mattia Parise <plis...@gmail.com>

Mattia Parise

unread,
Jan 30, 2012, 2:06:07 PM1/30/12
to Cinterion Java enabled chips support
Nikita, thanks for your help.
Can you give me a simple example about your idea of "leave a data
message"?
I don't understand what you are trying to explain me, sorry.
I really appreciated your help.
Thanks
Mattia

On Jan 30, 5:05 am, Nikita Kapitonov <pyth0n...@gmail.com> wrote:
> Mattia,
> Dyndns will help you only if you have public dynamic IP. It solves problem
> of dynamic vs static IP, but still you need public IP.
>
> Makuda is right, in most cases you don't have public IP, so you need to
> have the connection be initiated from mobile side.
>
> If you want some actions to be initiated from user (server) side, you
> should consider some kind of persistent connection (see Florent's M2MP
> protocol) to achieve like real-time speed of communication, or some kind of
> way to "leave a data message" on server, to be transerred to mobile side on
> next communication attempt, as I do it in my current project (it is very
> easy realized on plain HTTP/PHP, no need to run other servers).
>
> Best regards,
> Nikita
>
> 2012/1/27 Mattia Parise <pliski...@gmail.com>
>
>
>
> > Em,thanks for you reply.
> > And if I use a service called Dyndns on the chip?Could it work? I have
> > some code to get the chip be the dyndns updater.
> > My only question is...will it work or not?
> > And does this page ( at the 3rd point) tells the truth about the ip
> > address?
> >http://www.javameblog.com/2007/12/how-to-get-ip-address-of-mobile-usi...
> > Thanks in advance.
> > Mattia
>
> > On 27 Gen, 17:11, em <mak...@gmail.com> wrote:
> > > Hi,
>
> > > geLocalAddress() is correct method
> > > you're right the IP will be natted if you don't have pdp profile with
> > > public ip associated.
> > > You could use service likehttp://checkmyip.com/butdon't see any

Nikita Kapitonov

unread,
Jan 30, 2012, 11:05:38 PM1/30/12
to java...@googlegroups.com
Mattia,
that's very simple, but you need to know HTTP first

Idea is in the core of HTTP POST (or even GET) methods. When your mobile device is doing POST request to your server (to submit some data), the server must give some response. That is the place for you to give some additional data to your mobile device, if you have any.

Example (request):
POST /setvalue.php HTTP/1.1
Content-Length: 83

imei=353234012345678&param=2&time=2010-04-12 15:57:44&value=86.79&password=mPy0sk6e

Example (answer):
HTTP/1.1 200 OK
Server: nginx/0.6.31
Date: Mon, 12 Apr 2010 11:48:15 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.2.10

1a
0: OK; 2010/04/12,15:48:15; PLEASE DO SOME ACTION=1
0

Best regards,
Nikita

2012/1/30 Mattia Parise <plis...@gmail.com>

Mattia Parise

unread,
Jan 31, 2012, 5:52:55 AM1/31/12
to Cinterion Java enabled chips support
Hi Nikita,
thanks for your reply.
I understand a bit about what you are suggesting me.
I'll show you my idea so you can understand what I'm going to do.
To save battery, the chip won't be everytime connected in GPRS with a
server. It will open a SocketServerConnection only if it receive an
sms with that command.
Then is now that I have to know what is my IP connection, because now
I'm going to connect to Dyndns to tell it "Hey, it's me!" and Dyndns
will now know who the chip is. Then the client, with an application
(client side or in a website) will connect to the chip. This is the
way I'm going to work now. That's why Dyndns is not an option but I
need that, so that's why I am asking lots of questions about it.
I have only another question to ask.
The thing is that I know dyndns can see the internal Ip address ( i
know about that because there is an option in dyndns updater ).
Would this work?I was talking about this before. I don't know if this
is working, but my boss said that he had a problem with Dyndns some
time ago
because it saw only the internal ip address and not the public.
If this is not working, probably I would have one more question.
I'm going to study HTTP anyway, and I really appreciated your idea.
Thank you very much!
P.S. I was thinking about creating a STUN server to help the chip
knowing its IP, but probably that's not the best idea.



On 31 Gen, 05:05, Nikita Kapitonov <pyth0n...@gmail.com> wrote:
> Mattia,
> that's very simple, but you need to know HTTP firsthttp://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
>
> Idea is in the core of HTTP POST (or even GET) methods. When your mobile
> device is doing POST request to your server (to submit some data), the
> server must give some response. That is the place for you to give some
> additional data to your mobile device, if you have any.
>
> Example (request):
> POST /setvalue.php HTTP/1.1
> Host:www.myserver.ru
> Content-Length: 83
>
> imei=353234012345678&param=2&time=2010-04-12
> 15:57:44&value=86.79&password=mPy0sk6e
>
> Example (answer):
> HTTP/1.1 200 OK
> Server: nginx/0.6.31
> Date: Mon, 12 Apr 2010 11:48:15 GMT
> Content-Type: text/html; charset=UTF-8
> Transfer-Encoding: chunked
> Connection: keep-alive
> X-Powered-By: PHP/5.2.10
>
> 1a
> 0: OK; 2010/04/12,15:48:15; PLEASE DO SOME ACTION=1
> 0
>
> Best regards,
> Nikita
>
> 2012/1/30 Mattia Parise <pliski...@gmail.com>
> > > > > You could use service likehttp://checkmyip.com/butdon'tsee any

Nikita Kapitonov

unread,
Jan 31, 2012, 6:28:19 AM1/31/12
to java...@googlegroups.com
Hi Mattia,
you just don't understand the meaning of private IP and NAT technology.

Again, DynDNS will help you if you have dynamic PUBLIC IP without NAT. If you don't (in most cases you don't), it will not help you.

Knowing the internal IP address will not help you, because it is useless for a person ouside local network. In general case, you cannot connect to an internal IP from external network.

Knowing the public IP address when the mobile device is under NAT is useless also, because in general case, you cannot connect to a device under NAT.

So, you really don't need to know any of the IP addresses.
When your device receives an SMS, it have 2 ways to upload and receive data.

1. Establish real-time persistent connection. Here you can do some data exchange and then close the connection when don't need it anymore.
2. Establish instant HTTP session. Here you can in one data exchange do some upload and receive some previously made commands, then go sleep again.

Best regards,
Nikita

2012/1/31 Mattia Parise <plis...@gmail.com>

Mattia Parise

unread,
Jan 31, 2012, 8:33:31 AM1/31/12
to Cinterion Java enabled chips support
Nikita thanks for your reply.
You're right, I made a very big mistake this morning about this thing.
Sorry for the huge amount of questions, now all is clear, I know what
to do.
My boss said me a lot of wrong information, but now with your help I
know how to work.
Thank you very much for your help!
Best regards,
Mattia

On 31 Gen, 12:28, Nikita Kapitonov <pyth0n...@gmail.com> wrote:
> Hi Mattia,
> you just don't understand the meaning of private IP and NAT technology.
>
> Again, DynDNS will help you if you have dynamic PUBLIC IP without NAT. If
> you don't (in most cases you don't), it will not help you.
>
> Knowing the internal IP address will not help you, because it is useless
> for a person ouside local network. In general case, you cannot connect to
> an internal IP from external network.
>
> Knowing the public IP address when the mobile device is under NAT is
> useless also, because in general case, you cannot connect to a device under
> NAT.
>
> So, you really don't need to know any of the IP addresses.
> When your device receives an SMS, it have 2 ways to upload and receive data.
>
> 1. Establish real-time persistent connection. Here you can do some data
> exchange and then close the connection when don't need it anymore.
> 2. Establish instant HTTP session. Here you can in one data exchange do
> some upload and receive some previously made commands, then go sleep again.
>
> Best regards,
> Nikita
>
> 2012/1/31 Mattia Parise <pliski...@gmail.com>
> >http://groups.google.com/group/javacint?hl=en.- Nascondi testo citato
>
> - Mostra testo citato -

Florent Clairambault

unread,
Feb 1, 2012, 8:16:15 AM2/1/12
to java...@googlegroups.com
Hi Mattia, 

Protocols like M2MP are not intended to close their connection (ever). They are working on always open connections on a keep-alive fashion, so that they are always able to receive something. It makes very comfortable products because you can make frequent exchanges between your server and your deployed equipments pretty much like if you were talking to them through a serial link. Apple uses a similar protocol (MDM - http://media.blackhat.com/bh-us-11/Schuetz/BH_US_11_Schuetz_InsideAppleMDM_WP.pdf ) /service (APNS) for apps push notification and imessage.
What makes push protocol very interesting in terms of bandwidth is that NAT routers usually keep track of their translation table entries (between internal-IP+port and their external port for 30 minutes). So it means you only have to send a short message each 29 minutes to test if the link is still active.

However it makes the initial deployment and the maintenance of the system more difficult because you have to add a server specifically for this and you also need to setup some kind of data exchange between your core system and this server. In a lot of cases, using HTTP on a standard ASP//PHP/Java server makes everything easier. That's why I have deployed more projects using HTTP than M2MP.

Nikita gave a very basic example of communication but you can do some more advanced things like using XML or JSON in the content of an HTTP POST request and the in the server response. The only "difficult" part is that you always perform a http request from the client to the server and then get a response from the server to client. So making the server acknowledge data coming from the client is done instantly (in the response) but making the client acknowledge data coming from the server needs to be done in a second request.
You should note also that using gzip to compress your xml/gzip requests and answers can help you save a lot of bandwidth.

Sending data in your POST request is very simple, you just need to use the OutputStream of the HttpConnection you created. And you can use libraries to do on-the-fly compressing/uncompressing and format writing/parsing. 

The only big issue with HTTP is that you can tell the chip to apply settings (or report some sensors values) instantly. For that you would need to either reduce the frequency of your HTTP polling requests (which consumes some bandwidth), send a text message to your chip to order a HTTP request or even ring the chip to order a HTTP request.

Best regards,
--
Florent Clairambault

Mattia Parise

unread,
Feb 2, 2012, 4:40:56 AM2/2/12
to Cinterion Java enabled chips support
Hi Florent,
I'm probabily going to use VPN in this project.
Probably in the future I will try to use a STUN server ( UDP Hole
Punching ) that returns to the 2 clients their ip, so they can start a
p2p connection.
But now I think that the most straightforward way to deploy a p2p
connection is Makuda's idea: vpn.
Thank you all for your ideas and your support, I corrected a lot of
wrong concepts thanks to you.
Mattia

On Feb 1, 2:16 pm, Florent Clairambault
<florent.clairamba...@webingenia.com> wrote:
> Hi Mattia,
>
> Protocols like M2MP are not intended to close their connection (ever). They
> are working on always open connections on a keep-alive fashion, so that
> they are always able to receive something. It makes very comfortable
> products because you can make frequent exchanges between your server and
> your deployed equipments pretty much like if you were talking to them
> through a serial link. Apple uses a similar protocol (MDM -http://media.blackhat.com/bh-us-11/Schuetz/BH_US_11_Schuetz_InsideApp...
> )
> /service (APNS) for apps push notification and
> imessage<http://www.anandtech.com/show/4956/apple-ios-5-review/3>
> .
> ...
>
> read more »- Hide quoted text -
>
> - Show quoted text -

Nikita Kapitonov

unread,
Feb 2, 2012, 6:37:41 AM2/2/12
to java...@googlegroups.com
Hmm, I don't see any advantage of using VPN in that application. Imagine you finally got VPN connection, and you got mobile device's IP address, so what's next? You still need to develop some protocol to communicate with server.

As Florent suggests, "the most straightforward way" in that case will be HTTP-based communication with JSON-based data packets. Easy to develop, battery-effective, robust at possibilities. Really cool.

Why VPN?

Best regards,
Nikita

2012/2/2 Mattia Parise <plis...@gmail.com>

Nikita Kapitonov

unread,
Feb 2, 2012, 6:43:06 AM2/2/12
to java...@googlegroups.com
Florent, you said

You should note also that using gzip to compress your xml/gzip requests and answers can help you save a lot of bandwidth. 

That's an interesting idea! What library do you use on a mobile device to compress and decompress some data?

Best regards,
Nikita

2012/2/1 Florent Clairambault <florent.cl...@webingenia.com>

Florent Clairambault

unread,
Feb 2, 2012, 8:46:00 AM2/2/12
to java...@googlegroups.com
Yes,

VPN only solves the problem of establishing an asynchronous link from the server to the client and it might create some issues in the future as VPN sim card usually cost more and (make you dependant of the VPN).

I've succesfully used jazzlib http://jazzlib.sourceforge.net/ (i'm not sure I downloaded it from here through). It supports on-the-fly compression by offering a OutputStream wrapper. It has one major drawback though, by default it consumes about 200 KB of RAM, it's way too much. If you want to reduce it memory consumption, you have to change this :

Index: src/net/sf/jazzlib/DeflaterConstants.java
===================================================================
--- src/net/sf/jazzlib/DeflaterConstants.java (révision 766)
+++ src/net/sf/jazzlib/DeflaterConstants.java (copie de travail)
@@ -45,7 +45,7 @@
   final static int DYN_TREES    = 2;
   final static int PRESET_DICT  = 0x20;
 
-  final static int DEFAULT_MEM_LEVEL = 8;
+  final static int DEFAULT_MEM_LEVEL = 7;
 
   final static int MAX_MATCH = 258;
   final static int MIN_MATCH = 3;

And it reduced a lot the memory footprint of this library, I don't remember by how much but it was something like 50KB.

As I reduced the memory used for compression, I thought it was a good idea to increase the compression level (and take the risk of slowing down the compression) :

Index: src/net/sf/jazzlib/GZIPOutputStream.java
===================================================================
--- src/net/sf/jazzlib/GZIPOutputStream.java (révision 766)
+++ src/net/sf/jazzlib/GZIPOutputStream.java (copie de travail)
@@ -80,7 +80,7 @@
    */
   public GZIPOutputStream(OutputStream out, int size) throws IOException
   {
-    super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true), size);
+    super(out, new Deflater(Deflater.BEST_COMPRESSION, true), size);
     
     crc = new CRC32();
     int mod_time = (int) (System.currentTimeMillis() / 1000L);


A quick note about keep-alive connections: The overall power consumption isn't necessary superior to http requests. On a keep-alive TCP connection, you can keep your connections open for almost 30 minutes without any TCP/IP packet being transmitted. If you need to transmit data every 20 minutes, you save a lot of data transmitted (and power consumed) because you don't have to establish the TCP connection and send the HTTP headers.

Best regards,
--
Florent Clairambault


Mattia Parise

unread,
Feb 13, 2012, 5:43:03 AM2/13/12
to Cinterion Java enabled chips support
Hi to all.
I managed to do what I tryied using Jsch, creating an SSH reverse
tunnel and then creating a tunnel using the remote port.
Thank you for all your help.
Have a nice day!

On Feb 2, 2:46 pm, Florent Clairambault
<florent.clairamba...@webingenia.com> wrote:
> Yes,
>
> VPN only solves the problem of establishing an asynchronous link from the
> server to the client and it might create some issues in the future as VPN
> sim card usually cost more and (make you dependant of the VPN).
>
> I've succesfully used jazzlibhttp://jazzlib.sourceforge.net/(i'm not sure
> On Thu, Feb 2, 2012 at 12:43 PM, Nikita Kapitonov <pyth0n...@gmail.com>wrote:
>
>
>
> > Florent, you said
>
> > You should note also that using gzip to compress your xml/gzip requests
> >> and answers can help you save a lot of bandwidth.
>
> > That's an interesting idea! What library do you use on a mobile device to
> > compress and decompress some data?
>
> > Best regards,
> > Nikita
>
> > 2012/2/1 Florent Clairambault <florent.clairamba...@webingenia.com>
>
> >> Hi Mattia,
>
> >> Protocols like M2MP are not intended to close their connection (ever).
> >> They are working on always open connections on a keep-alive fashion, so
> >> that they are always able to receive something. It makes very comfortable
> >> products because you can make frequent exchanges between your server and
> >> your deployed equipments pretty much like if you were talking to them
> >> through a serial link. Apple uses a similar protocol (MDM -
> >>http://media.blackhat.com/bh-us-11/Schuetz/BH_US_11_Schuetz_InsideApp...)
> >> /service (APNS) for apps push notification and imessage<http://www.anandtech.com/show/4956/apple-ios-5-review/3>
> >> .
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages