The principle and difference between socket, tcp, http

Table of contents

The place of the various TCP/IP and other protocols in the original OSI model

Three-way handshake for TCP/IP connections

HTTP connection

SOCKET principle

Establish a socket connection

SOCKET connection and TCP/IP connection

Socket connection and HTTP connection

Summarize


As the core technology of network communication, socket, tcp and http play an important role in our daily life. In order to better understand them, we will discuss from two aspects of principle and difference.

The place of the various TCP/IP and other protocols in the original OSI model

  1. Application layer protocols: HTTP, SMTP, SNMP, FTP, Telnet, SIP, SSH, NFS, RTSP, XMPP, Whois, ENRP, etc.
  2. Presentation layer protocols: XDR, ASN.1, SMB, AFP, NCP, etc.
  3. Session layer protocols: ASAP, TLS, SSH, ISO 8327 / CCITT X.225, RPC, NetBIOS, ASP, Winsock, BSD sockets, etc.
  4. Transport layer protocols: TCP, UDP, RTP, SCTP, SPX, ATP, IL, etc.
  5. Network layer protocols: IP, ICMP, IGMP, IPX, BGP, OSPF, RIP, IGRP, EIGRP, ARP, RARP, etc.
  6. Data link layer protocols: Ethernet, Token Ring, HDLC, Frame Relay, ISDN, ATM, IEEE 802.11, FDDI, PPP, etc.

Please note that this is only one possible classification method, and the specific classification may vary according to the specific situation and needs.

Three-way handshake for TCP/IP connections

The three-way handshake process of a TCP/IP connection is as follows:

  1. In the preparation stage of sending data, the client sends a SYN packet to the server and enters the SYN_SENT state, waiting for the server to confirm.
  2. When the server receives the client's request, it sends a SYN packet to the client, and at the same time sends its own SYN packet, entering the SYN_RECV state.
  3. After receiving the two SYN packets sent by the server, the client sends a confirmation message ACK to the server. After sending, both the client and the server enter the ESTABLISHED state and complete the three-way handshake.

HTTP connection

HTTP connection involves two main processes: connection establishment and disconnection.

The process of establishing a connection is often referred to as the "three-way handshake":

  1. The client sends a TCP packet with the SYN (synchronous) flag, requesting to establish a TCP connection with the server.
  2. After the server receives the SYN packet, it sends a TCP packet with SYN and ACK (confirmation) flags, indicating that it has accepted the client's request.
  3. After receiving the SYN+ACK packet from the server, the client sends a TCP packet with the ACK flag to confirm that it has received the server's reply.

Once the connection is established, the HTTP protocol can send and receive data on the TCP connection. When the data transfer is complete, the client and server can send a TCP packet with the FIN (end) flag indicating that they wish to disconnect. This process is called "four waves."

HTTP connections are stateless, which means that every time a client makes a request, it must provide all connection information to the server, such as requested resources, protocol version, cookies, etc. The server cannot save the connection information with the client, so each request needs to obtain this information again.

HTTP connections can also be persistent, i.e. multiple requests can be sent over the same TCP connection. This approach reduces overhead and improves performance. Persistent connections can be achieved by setting "Keep-Alive" in the HTTP header.

In addition, the HTTP protocol also supports pipelined requests, that is, multiple requests are sent simultaneously in one TCP connection. This approach can further improve performance, but requires attention to the order and dependencies of requests.

In general, the HTTP connection is a reliable, stateless, persistent or persistent + pipelined transmission protocol based on the TCP protocol, which provides an efficient and universal data transmission method for Web applications.

SOCKET principle

The principle of Socket can be summarized into two main parts: server and client. The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket and then connects to the server (connect), if the connection is successful, then the connection between the client and the server is established. The client sends a data request, the server receives and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection, and an interaction ends.

Socket is an abstraction layer between the application layer and the transport layer, which hides the complexity of different operating system network protocols and provides a common interface for applications. Socket is actually a special I/O port for communication between processes. It provides a communication method across the network, enabling applications to exchange data on the network. In the design mode, Socket can be regarded as a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is all, let Socket organize data, and conform to the specified protocol.

Establish a socket connection

The process of establishing a Socket connection includes the following steps:

  1. Create a Socket object: On the client side, create a Socket object through the constructor of the Socket class, which needs to specify the IP address and port number of the server.
  2. Create a Socket object on the server side: the server side also needs to create a Socket object, which does not need to specify the IP address and port number, because it is used to monitor the client's connection request.
  3. The client sends a connection request: sends a connection request to the server through the created Socket object.
  4. The server side receives the connection request: the server side receives the client's connection request through the created Socket object, and then the two can perform data transmission.

It should be noted that before data transmission, it is necessary to ensure that the network connection is normal, otherwise data transmission cannot be performed. At the same time, when using Socket for communication, you need to pay attention to the format and protocol of data transmission to avoid communication errors.

SOCKET connection and TCP/IP connection

The Socket connection is a part of the TCP/IP protocol, which implements the TCP/IP communication mechanism, so that different programs can communicate through the network on different computers.

The TCP/IP protocol cluster is one of the core protocols of network communication, which provides a set of interfaces to enable applications to exchange data on the network. The TCP/IP protocol suite includes two main protocols, TCP (Transmission Control Protocol) and IP (Internet Protocol).

The TCP protocol is a reliable, connection-oriented protocol. It ensures the reliability and sequence of data transmission through steps such as establishing a connection, sending data, confirming receipt, and closing the connection. The IP protocol is responsible for sending data packets to the destination address, but it does not guarantee the order or reliability of data packet transmission.

The Socket connection is an abstraction layer between the application layer and the transport layer, which hides the complexity of network protocols of different operating systems and provides a common interface for applications. Socket connections allow two programs to communicate over a network on different computers without knowing the underlying TCP/IP protocol.

In a Socket connection, the client program sends a connection request to the server program by creating a Socket object. After receiving the connection request, the server program accepts the connection request and establishes a Socket connection. Once the connection is established, the client and server programs can transmit data through the Socket interface.

In short, the Socket connection is a part of the TCP/IP protocol, which provides a standard communication method that enables different programs to exchange data on the network. When using Socket for communication, we need to specify some parameters of Socket, such as IP address, port number, protocol, and so on.

Socket connection and HTTP connection

Socket connection and HTTP connection are different concepts. They correspond to the underlying TCP/IP protocol and the top-level HTTP protocol respectively, but they are both used to implement network communication at the application level.

The Socket connection is built on the TCP/IP protocol, which provides a standard interface that enables different programs to exchange data on the network. Socket connections allow two programs to communicate over a network on different computers without knowing the underlying TCP/IP protocol. In a Socket connection, the client program sends a connection request to the server program by creating a Socket object. After receiving the connection request, the server program accepts the connection request and establishes a Socket connection. Once the connection is established, the client and server programs can transmit data through the Socket interface.

The HTTP connection is built on top of the TCP/IP protocol, which is a simple protocol based on the request/response model. The HTTP protocol runs on top of the TCP protocol, uses TCP's three-way handshake to establish a connection, and then exchanges data between requests and responses. The HTTP connection corresponds to the application layer, which provides a standard communication method, enabling the client to send a request to the server and receive the server's response. HTTP connections are usually used in web applications to implement functions such as web page browsing and data transmission.

Generally speaking, both Socket connection and HTTP connection are used to realize network communication, but their application scenarios and communication methods are different. Socket connections are usually used to implement custom network communications, such as implementing custom protocols; while HTTP connections are used in web applications to implement functions such as web page browsing and data transmission.

Summarize

To sum up, socket, tcp and http, as the core technologies of network communication, each have unique principles and characteristics. Socket is the interface of the underlying communication, providing the mechanism of network communication, tcp is a reliable, connection-oriented protocol implemented on top of socket, and http is a simple request/response based on tcp Mode protocol, mainly used in web applications. Understanding their principles and differences will help us better apply and optimize network communication technologies.

Guess you like

Origin blog.csdn.net/wq2008best/article/details/132711657