Android socket implementation principle details how to build the server and client

This example shares the implementation principle of Android socket for your reference. The specific contents are as follows

Socket socket

It is the combination of IP address and port number with unique identification on the network that can form a unique identifier socket.

Principle and mechanism of socket implementation:

1. There are sockets at both ends of the communication

2. Network communication is actually the communication between sockets

3. Data is transmitted between two sockets through io

Establish socket (client) and ServerSocket (server)

After the connection is established, the data is transmitted through the IO stream in the socket

Close socket

Similarly, the client and server are two separate applications.

Server side thinking

1. To establish a socket service on the server side, a port is required. 2. The server does not operate directly, but obtains the client object through the accept method and communicates with the client through the stream of the obtained client object. 3. Read or write data through the method of obtaining the stream object through the client. 4. If the service is completed, the client needs to be closed, Then shut down the server. However, the client is usually shut down, not the server, because the server always provides services

Server implementation code

Client ideas

1. Establish the socket service of the client and specify the server to be connected. 2. If the connection is established successfully, it indicates that a data transmission channel has been established. Data can be read and written through IO in this channel. This channel is called socket stream, which contains both read and write streams. 3. The two streams can be obtained through the socket object method. 4. The objects passing through the stream can transmit data. 5. If the data transmission is completed, close resource

Client implementation code

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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