Public network server and LAN phone long connection

Source: Internet
Author: User

Transferred from: http://zhidao.baidu.com/link?url= 161oqiz7u-7q1lfp9vbaouaqfugpmfq889wvio-xf9i41dgtryhk3wngi361qm4j2lcfpx7nvvynu3lz1a89xb56p3xzyn77cbn7k-oio-_

This function is actually data synchronization, but also to consider the mobile phone itself, power, network traffic and other constraints, so there are usually on the mobile side of the two solutions:
1. One is to periodically go to the server to query data, usually using the HTTP protocol to access the Web server, called polling (polling);
2. There is also a long connection between the mobile side and the server, using the XMPP long connection, called push (push).
Push has a distinct advantage in terms of the amount of power, traffic, and latency of the data being consumed. However, the disadvantage of using push is:
For clients: Relatively high cost of implementation and maintenance, the maintenance of long connections under the mobile wireless network, relative to some technical development difficulty.
For the server: How to achieve multi-core concurrency, CPU job scheduling, a large number of long-connected concurrent maintenance and other technologies, there are still development difficulties.
Before describing the principles of the push scenario, let's look at the features of the mobile wireless network.
Features of mobile wireless networks:
Because IP v4 IP is limited, the operator assigned to the mobile phone terminal IP is the operator intranet IP, mobile phone to connect to the Internet, you need to do a network address translation through the operator's Gateway ("Networking Translation,nat"). Simply said that the operator's gateway needs to maintain an external network IP, port to the network IP, port correspondence, to ensure that the intranet phone can communicate with the Internet server
GGSN (Gateway GPRS
Supported node Gateway GPRS support node) module to achieve the NAT function.
Because most mobile wireless network operators are designed to reduce the load on the gateway's NAT mapping table, if it is found that there is no data traffic in the link, its corresponding table is deleted, causing the link to break. (about the role of NAT and its principles you can see my other blog post on the use of UDP (TCP) cross-LAN, NAT traversal experience)
Push on Android platform for long-connected implementations:
Now that we know that our mobile is going to communicate with the Internet, we have to go through the operator's gateway, so in order not to invalidate the NAT mapping table, we need to send the data to the Internet on a timed basis, because otherwise the NAT mapping table fails, so we only need to send the data with a length of 0.
This is the time to use the timer, on the Android system, timers usually have two kinds:
1.java.util.timer
2.android.app.alarmmanager
Analysis:
Timer: You can perform related tasks according to the schedule or time period. However, the timer needs to use Wakelock to keep the CPU awake to ensure the execution of the task, which consumes a lot of traffic, and when the CPU is dormant, it cannot wake up the task, so it is obviously inappropriate to apply it to the mobile side.
The Alarmmanager:alarmmanager class is a management class that belongs to the Android system packaged to manage the RTC module. This involves the RTC module, to better understand the difference between the two, you need to understand the real difference between the two.
RTC (Real-time clock) real-time alarms in an embedded system, usually using RTC
To provide reliable system time, including hours, minutes and months, and so on, it also requires the system to be in the shutdown state of the normal operation (usually backed up battery power), its periphery does not require too much auxiliary circuit, typically only need a high-precision 32.768KHz
such as crystal and resistor capacitance. (If you are interested in this, you can check the relevant information, here to say a ballpark)
Okay, back to the chase. So, Alarmmanager also called global timing alarm. This means that when I use Alarmmanager to perform tasks on a regular basis, the CPU can hibernate normally, and the CPU is awakened only when the task is performed, which is a very short process.
The following is a simple example of using:
1. Similar to the timer function:
Get the alarm Manager
Alarmmanager
am = (Alarmmanager) getsystemservice (Alarm_service);
Set up a task execution plan
Am.setrepeating (Alarmmanager.elapsed_realtime, Firsttime, 5*1000,
sender);//start execution from Firsttime and execute every 5 seconds
2. Implement the global timer function:
Get the alarm Manager
Alarmmanager
am = (Alarmmanager) getsystemservice (Alarm_service);
Set up a task execution plan
Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime,
5*1000, sender);//start execution from Firsttime and execute every 5 seconds
Summary: When using push push on an Android client, you should use Alarmmanager to implement the heartbeat feature to make it truly long-connected.

Have to learn to push ...

Public network server and LAN phone long connection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.