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 regi开发者_运维问答ster 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!
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.
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.
精彩评论