Hi. I am making an IM application for Android. The application consists of a server and client, both residing on the android. At startup, the applications register with a webserver. If a client wants to send an IM to a server behind a NAT router, how this be done? I heard of stun4j and jstun but found nowhere an example of their usage in android. Thanks!
stun4j is not a universal solution which will work for all NATs.
Simplest & most reliable solution is to send messaged via your external webserver with known address.
Why does the server have to be on the device?
This seems ineffective, android functions best as a typical client device.
Making an instant message app is quite simple.
Simply have the client send messages to the webserver, store them.
Have the other client(s) either wait or poll for messages from the webserver.
This will work regardless of the internet connection type.
If you're insisting on having the server on the device, I think the closest solution you'll get is to use some sort of upnp library.
With upnp you can just tell the router to automatically setup a NAT to your server's port.
This won't work for users who have disabled upnp in their routers though.
I had a similar problem in developing a soft client with IM. We licensed some software from a company called Eyeball Networks (www.eyeball.com) to solve the problem. They have a product called AnyFirewall that ensures that NAT traversal works properly through any NAT device. You can also look at their Messenger SDK which may streamline the development of your IM application, or you can use it to add VoIP and video.