3.2

socket

A socket is a wrapper around TCP/UDP connections. It provides an API for establishing and using TCP/UDP connections. Therefore, applications establish TCP/UDP connections through sockets.

File descriptor

In Linux, everything can be considered a file, and files can be divided into: normal files, directory files, link files, and device files. A file descriptor is an index created by the kernel to efficiently manage opened files. It is a non-negative integer (usually a small integer) that refers to the open file. All system calls to perform I/O operations go through the file descriptor.

A socket is a file descriptor

Socket The process of preparing a socket for establishing a connection

Server:

  • If a process wants to establish a TCP connection, it needs to go through the following three preparations
  • (1) Create socket object A —-> socket() method
  • (2) A binds the port number, which is usually applied for when the process is started. —–> bind(
  • (3) The process listens on A until A specific connection arrives —–> listen()

Client:

  • If a client expects to establish a TCP connection, there are two preparation periods
  • (1) Establish the socket object B —-> socket() method
  • (2) Initialize B, that is, fill the source IP address + source port number and destination IP address + destination port number —–> connect() method for B

Socket Process of setting up a connection through a socket (using TCP connections as an example)

(1) The connect() method of object B will send A TCP connection request to A. After A and B shake hands three times, the server will create A new socket C, and C and B will establish A connection. The client uses B and the process uses C to communicate.

(2) Socket A is blocked, waiting for the arrival of the next request, and the process will continue to listen to A.

Are multiple TCP connections created for the same port number?

Yes, the same port number can only be monitored by one socket socket A, but the same port number can be used to establish multiple TCP connections. The socket used by the server to establish the connection is newly generated by A. Each TCP connection consists of two sockets (one for the client and one for the server), which form a quad of client IP, Client port, Server IP, and Server port. The quad is used to distinguish different connections and uniquely identify TCP connections.

Communication is through the same port number. How can different connections accept their own data?

The operating system, when it receives data from a port, looks for a connection that matches this unique identifier and passes that information to the corresponding buffer. The way to find this is through the [client IP, client port, server IP, server port] quadtuple.

How many TCP connections can a port number establish?

If it is the same port number, it can establish as many TCP connections as the socket socket number, and a process will listen for this port number, that is, the process can use as many file descriptors, and establish as many TCP connections.

For example, Linux user processes can have a maximum of 1024 file descriptors by default. The kernel process has a maximum of 4096 file descriptors by default.

The process by which processes communicate using sockets

First, the client and server have a socket that has been established.

Client:

Use the send() method to send data

Server:

The data is received through the port, parsed to a quad, identified to the TCP connection, passed to the corresponding socket, and the process calls recv() to get the data.

When the communication is complete, the two sockets are disconnected after four waves, and both sockets fail.

On multiple established TCP connections on the same port, how can I know which TCP connection receives data?

A process uses the select, poll, and epoll methods to determine whether the TCP connection is transmitting data. These three methods belong to the CATEGORY of I/O multiplexing

Read and write queues of sockets

Each Socket has a read/write queue (storage space) associated with it in kernel space, a read queue and a write queue. The size of the read queue is generally larger than that of the write queue. The Socket reads data from the corresponding read queue and writes data to the corresponding write queue.

Can two processes listen on the same port?

Normally, a port can only be listened on by one process, but the Linux kernel supports port reuse, which allows two sockets to listen on the same port number at the same time, with port reuse options set. Two listener processes/threads can be used to listen on each of them at the same time, and connections sent by clients can be received in turn through round-robin balancing algorithms.

So port reuse is usually two identical processes reusing a port, otherwise the port data will be scrambled.