Android socket interface instant messaging instance code

Android socket interface for instant messaging

Recently, I learned the knowledge of Android communication. I'll take a small example to consolidate the learning content. The following content is the information found online. I think it's very good. The knowledge is relatively comprehensive. Let's have a look.

First, learn about the concept of instant messaging. The message object is transmitted through the message channel. One account is sent to another account. As long as the account is online, the message can be obtained immediately. This is the simplest communication. The message channel can be implemented by TCP / IP UDP. Generally speaking, it is a service that transmits the message objects (text, audio, video and files) that one person wants to send to another person through the message channel (C / s real-time communication). Instant messaging should include four forms: online direct transmission, online agent, offline agent and offline extension. Online direct transmission refers to the direct point-to-point transmission without passing through the server. Online proxy means that messages pass through the server, transit in the server, and finally reach the target account. Offline proxy means that messages are transferred through the server to the target account. When the other party is not online, messages are temporarily stored in the server's database and transmitted online. Offline extension refers to forwarding temporary messages to the target account in other forms, such as e-mail, SMS, etc.

In addition, we also need to understand the concepts related to computer network. In the classical four layer model of computer network, TCP and UDP are transport layer protocols, including message communication content. IP is a network layer protocol and a network address. TCP / IP, namely transmission control protocol / inter network protocol, defines the standard of how hosts connect to the Internet and how data is transmitted between them. Socket, also known as "socket", is an abstract layer between the application layer and the transport layer. It is used to describe the IP address and port. It is a handle for communication connection. Applications usually send requests or respond to network requests through "socket". It is the abstract representation of endpoints in the process of network communication. It abstracts the complex operation of TCP / IP layer into several simple interfaces. The supply layer calls the implemented processes to communicate in the network. XMPP (extensible message processing field protocol) is an application layer protocol based on extensible markup language (XML) and applied to instant messaging scenarios. The bottom layer is implemented through socket. It is used for instant messaging (IM) and online live detection. It facilitates quasi instant operation between servers. This protocol may eventually allow Internet users to send instant messages to anyone else on the Internet, even if their operating system is different from the browser. In this way, there are two schemes to realize instant messaging. One is to start with socket and directly use the interface provided by socket for data transmission. The second is to create a connection with xmppconnection with the help of open source tool (server openfire).

XMPP is a common way to realize instant messaging. In XMPP, all work is done by sending and receiving XMPP sections on an XMPP stream. The core XMPP toolset consists of three basic sections, namely < presence >, < message >, < IQ >. The XMPP stream consists of two XML documents, one for each direction of communication. This document has a root element < stream: stream >, whose child elements are composed of routable sections and top-level child elements related to streams. XMPP protocol also includes client and server. The client is developed based on Android platform. It is responsible for initializing the communication process. During instant communication, the client is responsible for initiating a connection creation request to the server. The system establishes a connection with the Internet through GPRS wireless network, and realizes instant communication with Android client through server. The server side uses openfire as the server. Allow multiple clients to log in at the same time and connect to a server concurrently. The server authenticates the connection of each client, creates a session for the authenticated client, and the communication between the client and the server is carried out in the context of the session. The instant messaging function realized by asmark open source framework is used. The framework is based on the open source XMPP instant messaging protocol, adopts C / S architecture, connects to the server with TCP protocol through GPRS wireless network, and sets up the open source openfn'e server as the instant messaging platform. XMPP message channel creation:

Configure the channel information to connect

Set debug information and security mode

The last step is to establish the connection

Observe the message change in the implementation class of contentobserver. Xmppconnection. Getroster() gets the contact list object. The general idea of writing communication protocol with XMPP protocol can be as follows. Enter the login interface, log in through the login method of xmppconnection, and enter the main interface successfully. The main interface contains two fragments, which are used to display contacts and chat records respectively. Create a data observer for contacts and SMS, set the listening rosterlistener() and chatmanagerlistener() in contacts and SMS services respectively, accept contact and SMS information, and add relevant information to the content provider. Set a content observer in the content provider to notify the interface to update when the data changes.

The focus of this paper is to use socket interface to realize instant messaging, because most of the bottom layer of instant messaging is realized through socket. The basic business logic can be described as follows. After entering the login interface, the user submits the account and password, which is determined by the server, and returns relevant parameters to determine the successful connection. Enter the chat interface or friends interface. Click the entry of contact or chat record to enter the chat interface. When the mobile terminal sends a message to the server again, the server forwards the message content to the target account. At the same time, the interface display is updated. This completes the basic functions of instant messaging. Of course, you can also add a background service to receive messages in the background when users launch the program. It is not difficult to see that for instant messaging, there are three concerns: message channel, message content and message object. Therefore, the main logic also revolves around these three points. The message channel realizes the sending and receiving of transmission message objects. Enter the address and port number of the socket (string host, int port) to create a connection. The format of message content should be consistent with that of the server. When receiving data, obtain the input stream, wrap it with datainputstream, and read the data sent by the server through the input stream. When sending data, obtain the output stream, wrap it with dataoutputstream, and send data to the server through the output stream. The message content should include sender, receiver information, data type, etc. The message object is the sender and receiver of the message. Next, we will explain it in detail in the code.

Create a message base class to realize the conversion of XML files and strings, and use the xsream third-party jar package. In this way, when you create a message class and inherit this method, you can directly realize the transformation of data in the class.

Create a thread tool that specifies that the method runs in the child thread and the main thread. Because the network operation needs to be in the sub thread and the interface update needs to be in the main thread, the thread creation tool can easily select threads.

Tool classes for creating messages, including message content, message type, message Province, etc. Since the content returned by the server contains the package name information of the message, the package name of the message itself should be maintained throughout the service.

About socket creation, connection, sending and receiving messages.

Layout of flash screen interface

Flash screen interface, hold for 4 seconds to enter the login interface. Generally speaking, the flash screen interface can load data, obtain version number, update version and other operations. It's not as complicated as it is.

Layout of login interface

Login interface, create a connection with the server, send login information to the server and accept the information returned by the server.

Friends list interface

The friends list timely receives the friends update information from which service it plays, and click the entry to jump to the chat interface.

Chat interface

Both message receiving and message sending in the chat interface need to update the list in time.

Finally, you can add a service to accept messages when the program exits.

Activity and service node configuration, and corresponding permissions.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>