Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Communication principle of TCP and socket

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

I. Network interconnection model

When the Internet was first introduced, only computers produced by the same manufacturer could communicate with each other. The purpose of establishing a network interconnection model is to provide a common foundation and standard framework for heterogeneous computer interconnection, and to provide a common reference for maintaining the consistency and compatibility of relevant standards.

Interconnected reference Model:

OSI seven-layer model (Open System Interconnect): application layer, presentation layer, session layer, transport layer, network layer, data link layer, physical layer

DoD four-layer model: a condensed version of the OSI seven-layer model, including process / application layer, host-to-host layer, Internet layer, network access layer

The above two models are hierarchical, and the main advantages of the hierarchical model are:

① divides the network communication process into smaller and simpler components, which contributes to component development, design and troubleshooting.

② defines the functions performed at each layer of the model, thus encouraging industry standardization

③ enables different types of network hardware and software to communicate with each other

④ avoids the modification of one layer to affect other layers

II. Transport layer protocol

At present, the mainstream transport layer protocols are TCP and UDP. SCTP is an improved version of TCP, and DCCP is a new unreliable transport protocol to replace UDP.

1. UDP (User Data Protocol, user Datagram Protocol): a connectionless protocol that provides simple and unreliable information transmission services for transactions, without establishing sessions, segmenting data, numbering or sorting, that is, it is impossible to know whether a message has arrived safely and completely after it has been sent. But it is precisely because the UDP protocol has few control options, low delay and high data transmission efficiency in the process of data transmission, so it is suitable for applications that do not require high reliability, or applications that can ensure reliability.

The commonly used UDP port numbers are: DNS 53, TFTP 69, SNMP 161.

2. TCP (Transmission Control Protocol, Transmission Control Protocol): a connection-oriented, reliable transport layer communication protocol based on byte stream.

TCP's mechanism for achieving reliability:

① establishes a connection between the two sides of the communication

② TCP splits the data flow into segments of messages of appropriate length (usually limited by the maximum transmission unit (MTU) of the data link layer of the network to which the computer is connected).

In order to ensure that packet loss does not occur, ③ TCP gives each message segment a sequence number, and the sequence number also ensures the sequential reception of the receiver. The arrival of TCP message segments may also be out of order. If necessary, TCP will reorder the received message segments and send the received data to the application layer in the correct order.

The ④ receiver sends back a corresponding acknowledgment (ACK) to the packet that has been successfully received; if the sender does not receive the acknowledgement within a reasonable round-trip delay (RTT), then the corresponding packet is assumed to have been lost and will be retransmitted.

⑤ TCP uses the checksum function to check whether there is an error in the message segment, and calculates the checksum when sending and receiving. If the receiver finds that there is an error in the checksum, it will discard the message segment and will not acknowledge the receipt of the message segment.

⑥ TCP also provides flow control: buffers (send buffer, receive buffer) and sliding window

⑦ adopts slow start and congestion avoidance algorithm in congestion control.

▲ TCP requires a three-way handshake to establish a connection before formally transferring data. The process is as follows:

The ① client sends a SYN (SEQ=x) message to the server and enters the SYN_SEND state.

The ② server receives the SYN message, responds to a SYN (SEQ=y) ACK (ACK=x+1) message, and enters the SYN_RECV state.

The ③ client receives the SYN message from the server, responds to an ACK (ACK=y+1) message, and enters the Established state.

▲ needs to terminate the connection after four handshakes after the data transmission is completed. The process is as follows:

① an application process first calls close (), saying that the end performs an "active shutdown" (active close). The TCP on this side then sends a FIN section (file Terminator) to indicate that the data has been sent.

The ② receives that the peer of the FIN performs a "passive shutdown" (passive close), and the FIN is confirmed by the TCP.

Note: the reception of FIN is also passed to the receiving application process as a file Terminator (end-of-file), after any other data that has been queued for the application process to receive, because the reception of FIN means that the receiving application process has no additional data to receive on the corresponding connection.

After ③ for a period of time, the application process that receives this file Terminator will call close to close its socket, causing its TCP to also send a FIN.

The ④ receives the original sender TCP of the final FIN (that is, the end that performs the active shutdown) to confirm the FIN.

The ▲ TCP protocol uses the tcp status to mark which stage of the communication process it is currently in:

CLOSED, LISTEN, SYN_SENT (active open), SYN_RECV, ESTABLISHED, FIN_WAIT1, CLOSE_WAIT, FIN_WAIT2, LAST_ACK, TIME_WAIT, CLOSED

III. Sockets

1. Socket: when the application layer communicates data through the transport layer, TCP and UDP will encounter the problem that multiple application processes provide concurrent services at the same time. There are three main parameters to distinguish the network communication and connection between different application processes: the destination IP address of the communication, the transport layer protocol (TCP or UDP) used, and the destination port number. This combination is called a socket.

Session:ip:portip:port

Port classification:

The well-known port: 0room1023, the administrator has the right to use it and is permanently assigned to an application.

Registration port: 1024041951: only some of them are registered, and the allocation is not particularly strict in principle.

Dynamic port or private port: 41952+

/ proc/sys/net/ipv4/ip_local_port_range: defines two digits that represent the starting and ending digits that can be used as temporary ports

2. Socket type:

Tcp socket (streaming)

Udp socket (Datagram)

Raw socket (applications at the application layer directly reach the IP layer without using TCP or UDP protocols)

3. Socket communication is implemented in domain. Different domain have different socket address formats. It is a file path name in unix domain, ipv4 address plus port number in ipv4 domain, and ipv6 address plus port number in ipv6 domain.

Domain:

Unix domain: a way to realize the communication between different processes of the same host based on socket mechanism; AF_UNIX,AF_LOCAL

IPv4 domain:AF_INET, based on socket mechanism, realizes the communication between processes on different hosts (but also on the same host) with the help of ipv4 protocol.

IPv6 domain:AF_INET6

Although ▲ IPv4 domain can realize inter-process communication between different hosts or the same host, Unix domain,socket should be used as far as possible for inter-process communication in the same host. The way to achieve communication in Unix domain is to create a read-write file in memory as an intermediary for inter-process interaction, which bypasses the network protocol stack, saves system resources and improves communication efficiency.

4. System calls related to tcp socket communication

Socket (): create a new socket

Bind (): tied to a socket address (used on the server side)

Listen (): listening sockets

Accept (): receives a connection request

Connect (): initiates a connection request

Close (): close the connection

Read () and write (): recv (), send (), recvfrom (), sendto ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report