Long Connection Architecture and Implementation in Android Clients

This article explains the principles of TCP long connections, compares NIO and BIO models, and describes how Android applications can implement and share persistent connections using AIDL, process sharing, and a dedicated SDK to support push, IM, and other real‑time services.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Long Connection Architecture and Implementation in Android Clients

Author Background : Yang Hongbo joined Qunar in 2014 and has been developing Android client applications, currently responsible for Android common functionality development and maintenance.

Basic Terminology

When using the TCP protocol, a connection must be established before read/write operations; this involves a three‑way handshake, and termination requires a four‑way handshake, consuming resources and time.

A "long connection" refers to keeping a single TCP connection open to send multiple data packets continuously. During idle periods, both sides send heartbeat packets to maintain the connection.

Although HTTP is a stateless, connection‑oriented protocol, the long connections discussed here are not HTTP keep‑alive connections but custom TCP connections that allow the server to control client and connection states, binding connection IDs to business IDs for targeted push notifications.

On Android, long connections enable scenarios such as user reminders, intelligent in‑app feedback, and instant messaging.

NIO Principles and Implementation

The classic blocking I/O (BIO) model creates a thread per connection, leading to high thread consumption because socket.accept(), socket.read(), and socket.write() are synchronous and blocking.

Non‑blocking I/O (NIO) uses a selector to manage multiple channels with a single thread. The typical NIO server workflow is:

1. Create a ServerSocketChannel and bind it to a port.

2. Create a Selector instance.

3. Register the ServerSocketChannel with the selector for OP_ACCEPT events.

4. Enter a loop:

4.1 Call select(), which blocks until one or more channels are ready for I/O.

4.2 Retrieve the selected key set.

4.3 Iterate over each key:

4.3.1 Obtain the channel and any attached object.

4.3.2 If the key is ready for accept, accept the connection, set the channel to non‑blocking, and register it with the selector.

4.3.3 Modify the key's interest set if needed.

4.3.4 Remove the processed key from the selected set.

The selector can monitor four event types: OP_CONNECT, OP_ACCEPT, OP_READ, and OP_WRITE.

Applications of Long Connections

A single long connection can be shared by multiple apps, allowing them to communicate with the server simultaneously. Common usages include:

1. AIDL : Implements inter‑process communication.

2. Process Sharing : Different apps share a process for communication; if the communication process already exists, a new one is not started. This requires configuration in AndroidManifest.xml (sample code omitted).

The overall system architecture enables one connection to serve multiple business lines through protocol families, receiving diverse server messages.

A Long‑Connection‑Centric Client SDK and Cloud Services

The platform provides a one‑stop long‑connection solution for various business lines, including IM cloud and push cloud. Business teams can integrate the SDK into a large client or a new app to gain offline push, instant messaging, and other features that would otherwise require extensive development.

Conclusion

Driven by operational demands, rising user expectations, and the rapid growth of big data and AI, long‑connection scenarios are expanding beyond push and chat. As back‑end systems gain deeper insight into client behavior, the need for stable, comprehensive long‑connection solutions will continue to increase, inviting further discussion and collaboration.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentAndroidnioNetworkinglong-connection
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.