Why Socket? People who have read my articles on the Bluetooth protocol (1) and the Bluetooth protocol (2) know that the app I want to use is to read the data in the Bluetooth pen to the mobile phone, the Socket based on the Bluetooth protocol is used, so I want to talk about Socket programming here.
I. What is Socket?
It is actually an interface for network communication. Based on different protocols, there are various sockets, for example, a Socket based on the TCP protocol, a Socket based on the UDP protocol, and a Socket based on the Bluetooth protocol, Android uses the Java Socket model.
In the computer industry, Socket is often called "Socket". It is used to describe IP addresses and ports and is a communication chain handle. This is a relatively abstract concept. A computer has a port. Each port can have an application for communication. For example, port 80 is the port used by the HTTP protocol, port 21 is the port used by the FTP protocol, and the port is the interface for communication between the computer and the outside world. These interfaces are logical interfaces, the port number ranges from 0 to 256 multiplied by 256 minus 1. The ports below are reserved ports used by the operating system. The above ports can be used freely, do not conflict with the ports of other applications.
An application can send a request to or respond to a network request through a "Socket". At this time, the Socket is divided into two parts, one of which is the Socket at the server end, this Socket is mainly used to receive requests from the network. It has been listening to a port. One part is the Socket of the client, which is mainly used to send data to the network.
Ii. Socket Communication Model
-UDP and TCP
UDP and TCP are the two most widely used protocols on the Internet, both of which are IP-based. The first difference is that the UDP protocol is not very reliable. The UDP Protocol divides all data into data packets, and the packet comes with the communication address, that is to say, which address I want to send this packet to on the network and send it out through the network. Whether the packet is sent to the destination or whether the server receives the packet, this agreement is not guaranteed. Just like China Post, you sent the mail, but the postal system does not guarantee that the recipient can receive the mail you sent. When sending data over TCP, the receiver is required to send a response after receiving the data, that is, whether or not you have received the data. TCP is more reliable, TCP is generally used when we send important data. Another difference is that the capacity of a packet sent by the UDP protocol is limited, while the TCP protocol does not. It does not mean that the UDP protocol must be inferior to the TCP protocol. It is used in different fields. The advantage of the UDP protocol is that the speed is relatively faster. TCP is relatively slow.
-Socket communication process
One of the two protocols can be used by an application through "Socket", that is, Socket. You can use UDP to send data or TCP to send data, data is sent to the server (receiving end) through the "communication channel", that is, the basic network of the IP address. The UDP protocol is used for sending data, and UDP protocol is used for receiving data. TCP protocol is used for sending data, and TCP protocol is used for receiving data, you can specify the IP address and port number of the receiver when sending data packets or data. The framework has helped us encapsulate the data packets or data.