Back to Blog

The Relationship Between Socket Connections and TCP Connections

When transmitting data, we can use only the (transport layer) TCP/IP protocol. However, without an application layer, the data content cannot be recognized. For the transmitted data to be meaningful, an application layer protocol must be used. There are many application layer protocols, such as HTTP, FTP, TELNET, etc., and custom application layer protocols can also be defined. The web uses the HTTP protocol as the application layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it over the network.

  1. A Socket is an interface for TCP and UDP programming; you can use it to establish TCP connections and so on. TCP and UDP protocols belong to the transport layer. HTTP, on the other hand, is an application layer protocol, and it is actually built on top of the TCP protocol. (HTTP is like a car, providing the specific form for encapsulating or displaying data; Socket is like an engine, providing network communication capabilities.)

  2. A Socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but an application programming interface (API). It is through Sockets that we can use the TCP/IP protocol. The emergence of Sockets simply makes it more convenient for programmers to use the TCP/IP protocol stack; it is an abstraction of the TCP/IP protocol, thus forming some of the most basic function interfaces we know.

Correspondence between SOCKET API and TCP STATE__Three-way handshake (listen, accept, connect)__Four-way handshake close and TCP delayed acknowledgment (calling setsockopt once, setting TCP_QUICKACK)__Long-lived connection API beware of 'packet interleaving' issues