Real time communication (socket) that must be understood by Andrews

Socket:

It can be divided into server and client. It is the encapsulation of TCP / IP. It uses IP address and port to determine a unique point. Hosts on the Internet generally run multiple service software and provide several services at the same time. Each service opens a socket and is bound to a port. Different ports correspond to different services. It should be noted that the port used by the user should be greater than 1024, because most ports less than 1024 are occupied by the system. This chapter will implement Android socket client programming.

Android thread basic mechanism

A program is a process. There can be multiple threads in a process, and each process must have a main thread. An application corresponding to Android is a process, and its main thread is the so-called Android main UI thread. When Android implements multithreaded programming, an important principle is that the UI update must be in the main thread, but the time-consuming operation must be in the sub thread. If the time-consuming operation is written in the main thread (such as network access), when the blocking time reaches a certain value, the application will be forced to exit. Then the network access is faced with an inevitable problem: how to implement the UI update operation of the sub thread.

Handler

Handler is mainly used for asynchronous message processing: it is a bit like an auxiliary class, encapsulating interfaces such as message delivery and message processing. When a message is sent, it first enters a message queue, and the function sending the message returns immediately, while the other part takes out the messages one by one in the message queue, and then processes the messages, that is, sending and receiving messages are not processed synchronously. This mechanism is usually used to deal with relatively time-consuming operations.

Message

The message object received and processed by the handler, where the message types are

Public int arg1 and public int arg2: store simple integer type messages

Public object obj: any object sent to the receiver, whether integer, string or a class object

Public int what: user defined message code, so that the receiver can understand the message information. Each handler contains its own message code, so there is no need to worry about the conflict between the customized message and other handlers.

Andrews end to achieve results

A device in the same network starts listening on a port as a socket server, obtains the IP address and port number of the server device, formats it as "IP: Port" for input, such as "193.169.44.198:8081", and then click Connect. Android interacts with the server as a socket client.

Programming implementation

Get network access:

To realize socket programming, you must turn on network access

Write the handler message processing class:

Handler message processing class is the internal class of mainactivity class. When the message queue is not empty, it will automatically enter, obtain the message value and analyze the content

Connect button listening:

When the connect button is pressed, the contents of the input box will be immediately obtained and separated by strings to obtain the IP address and port number, and the thread will be started for network connection

Send data button listening:

When the send data button is pressed, the contents of the send input box will be obtained immediately. You can call the string send function and hexadecimal send function to send data respectively

Start network connection thread:

This class is the internal class of mainactivity class, which enables the thread to connect to the socket server, obtain the input and output stream, and start the receiving thread

Close socket function:

Before closing the socket, the I / O stream will be closed first, so that the socket can be closed more safely

Data receiving thread implementation:

The receiving thread will receive the data and send the received data to the processing class through a message. In particular, it should be noted that INX. Read (BU) returns - 1, which indicates that the server is disconnected or an error caused by other non active calls to close the socket method

Send string function:

The final sending content of network programming is bytes, so the sending string needs to be encoded through GetBytes

Send hex function:

Through byte array, multiple hexadecimal data can be sent

reference resources:

https://blog.csdn.net/rabbit_ in_ android/article/details/50585156

https://www.imooc.com/article/25134? block_ id=tuijian_ wz

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
分享
二维码
< <上一篇
下一篇>>