Sockets (Linux-related)

Source: Internet
Author: User

Preface: A brief

First, the Antecedent

Everything starts with TCP and UDP.

As we all know, the network model generally has two models, one is the OSI conceptual model (layer seven) and the other is the TCP/IP network model (four tiers).

TCP/IP Application layer corresponds to OSI application layer, display layer, Session layer, TCP, UDP corresponding transport layer, Internet layer corresponds to network layer, device driver and hardware correspond to physical layer. As shown: '

  

On Linux, the four layer below is called the communication subnet , above four layer is called the application layer . The former pays more attention to the details of communication , mainly realizes how to transmit the data message between host and host, the latter pays more attention to the application layer details , mainly realizes the exchange of resources .

After Linux starts, the application layer details are implemented in the user space , and the communication details are implemented in the kernel space .

Therefore, the most original and most required inter-host communication is done by the kernel , the relatively real application is the communication protocol in the user-space-specific implementation , such as DNS to complete the domain name resolution, such as SSH to achieve secure landing. Naturally, you should not be in kernel space when you are completing a specific function .

Of course, there is also some doubts, the communication between the host is not through the IP address of the communication between? That's right. However , the IP protocol itself is not reliable and is not connected . Relatively speaking, the TCP protocol is a link, the transport layer to some extent to compensate for the network layer defects, of course, this is only part of the reason.

Second, the subject

Back to the point, the so-called Transport layer (TCP, UDP) is to provide the process address space, using the port number to identify the process address space.

TCP is a Transmission Control protocol, that is, object-oriented protocols, the need to establish a virtual link before communication, you need to know in advance whether the other side is online, after the end of the communication must be disconnected (TCP three handshake).

While UDP (User Datagram Protocol) is a no-connection protocol, there is no need to establish a virtual link before communication, no need to determine whether the other side is online.

The two are completely different protocols, and each has 0-65535 ports.

As mentioned above, all application layer protocols are located in the application layer, and if you need to communicate across hosts, you must apply to the kernel, however ... However, the kernel marks the communication between the hosts through the network, but it is done by socket .

So the question comes again, what is the socket?

Socket (SOCKET): an implementation of IPC; The primary purpose is to allow communication between different processes on different hosts (or consent hosts) to complete the exchange of data. Admittedly, sockets are implemented by the kernel and belong to the kernel, which belongs to system call. In fact, the socket is generally referred to as the socket API, mainly because it provides too low-level base in system call, so it is called the socket API in the C standard library.

So the realization is that when the user initiates inter-host communication, the socket is called based on the socket, and the kernel space requests the TCP, UDP port to establish the virtual link (if it is TCP) to communicate.

  

Aside from TCP and UDP, there is another way to encapsulate IP packets directly using bare sockets .

Three types of sockets:

       SOCK_STREAM:TCP sockets

       SOCK_DGRAM:UDP sockets

       Sock_ram: Bare Socket

To sum up, no matter what network services, must use the basic functions of network communication. That is, TCP, UDP, or other.

  

Sockets (Linux-related)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.