[Android Development] How does one achieve persistent connection between Android and servers? How messages are pushed

Source: Internet
Author: User

Preface:Most mobile applications now have the ability to get messages in real time. Simply put, they have the initiative to send messages and the passive right to receive messages. For example, QQ, weather forecast, etc. Believe in benefits and user experience.

Raise questions:This function must involve the client and server. So how does the client implement real-time connection and communication with the server?

Analyze the problem:This feature is actually data synchronization. We also need to consider the mobile phone itself, power, network traffic, and other restrictions. Therefore, there are usually two solutions on the mobile terminal:
1. One is to regularly query data from the server. Generally, the HTTP protocol is used to access the Web server, called polling );
2. Another method is to establish a persistent connection between the mobile terminal and the server. The XMPP persistent connection is called push ).

In terms of power consumption, traffic, and data delay, push has obvious advantages. However, the disadvantage of using push is:
For clients: the implementation and maintenance costs are relatively high. Maintaining persistent connections in mobile wireless networks is technically difficult to develop.
For servers: there are still development difficulties in how to implement multi-core concurrency, CPU job scheduling, and a large number of Long-connection concurrent maintenance technologies.

Before describing the principles of the push solution, let's take a look at the features of the mobile wireless network.
Features of mobile wireless networks:
Because the IP address V4 has a limited number of IP addresses, the IP address assigned to the mobile terminal by the operator is the Intranet IP address of the carrier, and the mobile phone needs to connect to the Internet, you need to perform a Network Address Translation (NAT) through the carrier's gateway ). To put it simply, the carrier's gateway needs to maintain an Internet IP address, Port-to-Intranet IP address, and port correspondence to ensure that the Intranet mobile phone can communicate with the internet server.
 
The schematic diagram is as follows:

 



The ggsn (Gateway GPRS Support Node) module implements the NAT Function.
Most mobile wireless network operators are trying to reduce the load on the NAT ing table of the Gateway. Therefore, if a link does not have data communication for a period of time, the corresponding table will be deleted, resulting in link interruption. (For more information about the role and principles of NAT, see my other blog post: Experiences on using UDP (TCP) for cross-lan and Nat penetration)
 
Implementation of push persistent connections on the Android platform:
Since we know that the mobile end must communicate with the Internet through the carrier's gateway, in order not to invalidate the NAT ing table, we need to regularly send data to the Internet, because the NAT ing table is invalid, you only need to send data with a length of 0.

At this time, you need to use a timer. In Android, there are usually two types of Timers:
1. java. util. Timer
2. Android. App. alarmmanager

Analysis:
Timer: related tasks can be executed according to the plan or time period. However, timer needs to use wakelock to keep the CPU awake to ensure task execution, which consumes a lot of traffic. When the CPU is in sleep state, it cannot wake up and execute the task, therefore, it is obviously inappropriate to apply it to mobile terminals.

Alarmmanager: The alarmmanager class is a management class encapsulated by the Android system to manage the RTC module. The RTC module is involved here. To better understand the differences between the two, you must understand the real differences between the two.
RTC (Real-time clock) Real-time alarms are usually used in an embedded system to provide reliable system time, including time, minute, second, and year, month, and day; in addition, it is required that the system be shut down to work properly (usually powered by a backup battery), and its peripheral does not require too many auxiliary circuits, typically, only a high-precision 32.768 kHz crystal and resistance capacitor are needed. (If you are interested in this aspect, you can check the relevant information. Here is a rough description)
Now, let's get back to the question. Therefore, alarmmanager is also called a global scheduled alarm. This means that when I use alarmmanager to regularly execute tasks, the CPU can sleep normally and wake up the CPU only when the task is executed. This process is very short.
The following is a brief description of its usage:
1. Similar to the timer function:
// Obtain the alarm Manager
Alarmmanager AM = (alarmmanager) getsystemservice (alarm_service );
// Set the task execution plan
Am. setrepeating (alarmmanager. elapsed_realtime, firsttime, 5*1000, Sender); // The task starts from firsttime and runs again every five seconds.

2. Global timing:
// Obtain the alarm Manager
Alarmmanager AM = (alarmmanager) getsystemservice (alarm_service );
// Set the task execution plan
Am. setrepeating (alarmmanager. elapsed_realtime_wakeup, firsttime, 5*1000, Sender); // The task starts from firsttime and is executed every 5 seconds.

Summary:When the android client uses push, alarmmanager should be used to implement the heartbeat function so that it can truly implement persistent connections.

Server implementation:
On the server side, it can be implemented in many languages, such as C/C ++, Java, and Erlang, such as Aurora push (C development) and openfire (Java Development) and so on.
Recently I have read the introduction and principles of Aurora push. Next I will talk about what problems they encounter and what technologies or solutions they will use to solve them.

When a large number of mobile terminals need to maintain persistent connections with the server, the design of the server will be a great challenge.

Assume that one server maintains 0.1 million persistent connections. When there are 10 million users, up to 100 servers are required to maintain these users' persistent connections. This is not a backup server, this will be a huge cost issue. We need to increase the number of users connected to a single server as much as possible, that is, the c10k issue that has been discussed for a long time in the industry.
C2000k

To address this problem, they set up a special project named c2000k. As the name suggests, they aim to maintain 2 million persistent connections on a single machine. In the end, they adopted a multi-message loop and asynchronous non-blocking model. On a dual-core, 24 GB memory server, they achieved a peak value of more than 3 million persistent connections.

Conclusion:
I recently used Java to build an instant messaging system for PCs, servers, and Android (to put it bluntly, it imitates QQ and I hope to have different functions later ). My principle is to use the principle of others and implement it on your own, so that you can gain a better understanding of some frameworks. Therefore, it is difficult to estimate that communication development and server development require a deep understanding of the Multi-message loop and asynchronous non-blocking model. I will post on the implementation in this regard later.
On the Current Android platform, it is no longer the world of Android standalone (I am not saying there is no future for single-host games ). Nowadays, the whole IT system is supported by the booming Internet. To become an Android Application Development Master, we must develop towards the Android, hardware, and cloud services system.


Related Article

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.