Good articles to my personal technology blog: https://cainluo.github.io/14986613643920.html


The interpretation of the Socket

A Socket is a Socket. A Socket is a Socket. A Socket is a Socket.

Socket is the cornerstone of communication and the basic operation unit of network communication that supports TCP/IP. It contains five kinds of information necessary for network communication: the protocol used for connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol port of the remote process.

In practice, there must be more than one TCP/IP or application process transmitting data. To distinguish this, the system provides a set of APIS for TCP/IP to interact with applications. This is commonly called the Socket API.

The application layer can communicate with the transport layer through the Socket API to distinguish between different application processes or network communication, so that data can be transmitted concurrently.


The Socket of the C/S side

In fact, the use of Socket is not as difficult as we imagined, but there is a prerequisite, because the establishment of Socket communication requires at least a pair of sockets, one of which is to run on our Client, called Client Socket, and another is to run on the server. Known as the Service Socket.

The connection between sockets is divided into three processes:

  • Server listening
  • Client request
  • Connect to confirm

Socket supports both TCP and UDP.

When a Socket uses TCP to communicate, the Socket is a TCP connection. If the Socket uses UDP to communicate, the Socket is a UDP connection.

If I had a picture, it would look something like this:


Network protocol

Since down to Socket, then it is about to talk about the network inside the protocol, here is about to popularize the network inside what in the end, first of all, we need to know that there are seven layers of protocol in the network.

From the top down:

  • The application layer
  • Express layer
  • The session layer
  • The transport layer
  • The network layer
  • Data link layer
  • The physical layer

However, we usually refer to the bottom three layers (network layer, data link layer, physical layer) as the media layer, which is generally the research object of network engineers specializing in network.

The top four layers (transmission layer, session layer, presentation layer, application layer) are called the host layer, but also the majority of users oriented and concerned about the content.

Speaking of protocols, we have to mention HTTP:

  • HTTPProtocol: it is corresponding toThe application layer.
  • TCPProtocol: it is corresponding toThe transport layer.
  • IPProtocol: corresponds toThe network layer.

HTTP is based on TCP connections, so there is no comparison.

  • TCP/IPProtocol: a transport layer protocol that resolves how data is transmitted on the network.
  • HTTPProtocol: Belongs to the application layer, mainly solve how to wrap data.

Socket is the software abstraction layer in the communication between application layer and TCP/IP protocol family, and it is a group of interfaces.


TCP/IP five-tier model

Now that TCP/IP is mentioned, let’s talk about the five-tier model in TCP/IP.

The five layer models are as follows:

  • The application layer
  • The transport layer
  • The network layer
  • Data link layer
  • The physical layer

So what are the network devices we are exposed to in our daily life? Which model are they in?

  • Physical layer: repeater, hub, twisted-pair
  • Data link layer: Ethernet switch (Modem), network adapter (half of the network adapter works at the physical layer)
  • Network layer: router, layer 3 switch
  • Transport layer: Four layer switches, routers (there are also some special routers that work in the transport layer)

Among them, TCP/IP application layer is special, which contains the session layer, expression layer, application layer and other functions. Here, IT should be said that TCP/IP protocol can not always ensure reliable transmission of data packets in the transmission layer, but the seven-layer model can do it.

TCP/IP also provides an alternative transport mode called UDP(User Datagram Protocol), but this protocol does not guarantee reliable packet transmission.

Here’s another explanation:

  • TCP: Connection-oriented, provides reliable data transmission and is used to transfer large amounts of data. The data flow mode is used, and the connection speed is slow and the connection cost is high
  • UDP: non-connection-oriented, unreliable transmission, used to transfer a small amount of data, fast

Generally speaking, TCP is a datagram protocol, while UDP is a datagram protocol.

Socket is an abstraction based on TCP/IP protocol. That is to say, a Socket is only the encapsulation of TCP/IP. There is no protocol in itself.

Here’s the schematic diagram:


conclusion

Although this article does not involve any code, it provides some basic knowledge. If you want to learn more about it or find a more official document, you can check out the Networking Overview provided by Apple dad.


The last