Android phone development - UDP services

2020-07-18 07:16发布

问题:

I'm interested in the viability of implementing an Android application that interacts with a UDP service. I'm sure Android supports UDP:

http://www.helloandroid.com/tutorials/simple-udp-communication-example

What I'm less clear about is whether or not such an application would actually work on an Android phone on a typical tariff - for bidirectional communication. I want the Android client to contact a remotely hosted service by UDP (announcing itself) and for the server to subsequently notify the Android device about real-time events.

I'm concerned that a mobile device on a commercial network might be forced to change IP address - for example - when moving in/out of a Wi-Fi zone... and I'm unclear if NAT would be a problem on a typical 3G network.

Is it really viable to use UDP for bi-directional (best-effort) communications both to and from an Android phone?

回答1:

There are a few issues for you to look at here. First, UDP is perfectly useable by Android. It can send and receive UDP packets just fine. The larger issue is really that the phone cannot receive incoming UDP connections that it did not open the initial connection for, unless it is on WiFi and rhe connection is coming from the same LAN.

On 3G/4G the phone is not addressable from the outside because the Wireless carrier has in place a firewall with a proxy server. So, incoming connections are not passed over that firewall. If, instead the phone opens the connection from behind the firewall, then bidirectional traffic can flow over that link.

Now, the problem you will have will be

1) Keeping the link alive when the phone goes from network (3G) to network (Wifi)

2) Battery life from the constantly open connection

Most likely, you really want C2DM for what you are looking at.