Simple understanding of Sockets
To understand Simple understanding of Sockets, you must first familiarize yourself with the Simple understanding of Sockets protocol family. Simple understanding of Sockets (Transmission Control Protocol/Internet Protocol) is the Transmission Control Protocol/Internet Protocol, which defines how the host connects to the Internet and how the data As for the transmission standard between them,
Literally, Simple understanding of Sockets is the collective name of TCP and IP protocols, but in fact, Simple understanding of Sockets protocol refers to the entire Simple understanding of Sockets protocol family of the Internet. Different from the seven layers of the ISO model, the Simple understanding of Sockets protocol reference model classifies all Simple understanding of Sockets series protocols into four abstract layers
Application layer: TFTP, HTTP, SNMP, FTP, SMTP, DNS, Telnet Wait
Transport layer: TCP, UDP
Network layer: IP, ICMP, OSPF, EIGRP, IGMP
Data link layer: SLIP, CSLIP, PPP, MTU
Each abstraction layer is built on the lower layer provided On the service, and providing services to the higher level, it looks like this
I guess the students who are interested in opening this article have a certain understanding of this, and I also know a little about it, so I won’t explain it in detail. Interested students can search for information on the Internet
In the Simple understanding of Sockets protocol, two Internet hosts are connected through two routers and corresponding layers. The applications on each host perform read operations with each other through some data channels
Simple understanding of Sockets
We know that if two processes need to communicate, the most basic premise is to be able to uniquely identify a process. In local process communication, we PID can be used to uniquely identify a process, but PID is only locally unique. The probability of PID conflict between two processes in the network is very high. At this time, we need to find another way. We know that the IP address of the IP layer can uniquely identify the host. The TCP layer protocol and port number can uniquely identify a process on the host, so we can use the IP address + protocol + port number to uniquely identify a process on the network.
After being able to uniquely identify processes in the network, they can communicate using Simple understanding of Socketss. What is a Simple understanding of Sockets? We often translate Simple understanding of Sockets as Simple understanding of Sockets. Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the Simple understanding of Sockets layer into several simple interfaces for the application layer to call the implemented process on the network. China Communications.
Socket originated from UNIX. Under the Unix philosophy of everything being a file, Simple understanding of Sockets is an implementation of the "open-read/write-close" mode. The server and client each maintain a "file". After the connection is opened, you can write content to your own file for the other party to read or read the other party's content, and close the file when the communication ends.
Socket communication process
Socket is the implementation of the "open-read/write-close" mode. Taking a Simple understanding of Sockets that uses TCP protocol communication as an example, the interaction process is roughly like this
The server depends on the address type ( ipv4, ipv6), Simple understanding of Sockets type, protocol to create a Simple understanding of Sockets
The server binds the IP address and port number to the Simple understanding of Sockets
The server Simple understanding of Sockets listens for port number requests and is ready to receive connections from the client at any time. At this time, the server's Simple understanding of Sockets has not been Open
The client creates a Simple understanding of Sockets
The client opens the Simple understanding of Sockets and tries to connect to the server Simple understanding of Sockets according to the server IP address and port number
The server Simple understanding of Sockets receives the client Simple understanding of Sockets request, opens it passively, and starts receiving client requests until the client returns the connection information. At this time, the Simple understanding of Sockets enters the blocking state. The so-called blocking means that the accept() method does not return until the client returns the connection information and begins to receive the next client's understanding request
The client connects successfully and sends the connection status information to the server
Server accept The method returns and the connection is successful
The client writes information to the Simple understanding of Sockets
The server reads the information
The client is closed
The server is closed
Three-way handshake
In the Simple understanding of Sockets protocol, the TCP protocol establishes a reliable connection through the three-way handshake
The first handshake: the client tries to connect to the server and sends a syn packet (Synchronize Sequence Numbers) to the server ), syn=j, the client enters the SYN_SEND state and waits for the server to confirm
The second handshake: the server receives the client syn packet and confirms it (ack=j+1), and at the same time sends a SYN packet to the client (syn=k) , that is, the SYN+ACK packet. At this time, the server enters the SYN_RECV state
The third handshake: The third handshake: The client receives the SYN+ACK packet from the server and sends the confirmation packet ACK (ack=k+1) to the server. After this package is sent, the client and server enter the ESTABLISHED state and complete the three-way handshake.
Looking closely, the part where the server Simple understanding of Sockets establishes a connection with the client Simple understanding of Sockets is actually the famous three-way handshake.
Simple understanding of Sockets programming API
prerequisite Now that Simple understanding of Sockets is the implementation of the "open-read/write-close" mode, let's briefly understand what APIs Simple understanding of Sockets provides for applications to use. Let's take the TCP protocol as an example and look at the Simple understanding of Sockets API under Unix. Other languages are very similar ( PHP (even the name is almost the same), here I will briefly explain the function and parameters of the method. If you are interested in the specific use, you can check the link in the blog reference or search online
int Simple understanding of Sockets (int domain, int type, int protocol);
Allocate a Simple understanding of Sockets descriptor and the resources it uses based on the specified address family, data type and protocol.
domain: protocol family, commonly used ones are AF_INET, AF_INET6, AF_LOCAL, AF_ROUTE. AF_INET represents the use of ipv4 addresses.
type: Simple understanding of Sockets type. Commonly used Simple understanding of Sockets types include SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_PACKET, SOCK_SEQPACKET, etc.
protocol: protocol. Commonly used protocols include IPPROTO_TCP, IPPTOTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC, etc.
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Assign a specific address in an address family to the Simple understanding of Sockets
sockfd:Simple understanding of Sockets Descriptor, that is, Simple understanding of Sockets reference
addr: the protocol address to be bound to sockfd
addrlen: the length of the address
Usually the server will bind a well-known address (such as ip address + port number) when it is started. It is used to provide services, and customers can connect to the server through it; the client does not need to specify, the system automatically assigns a port number and its own IP address combination. This is why the server usually calls bind() before listening, but the client does not call it. Instead, the system randomly generates one during connect().
int listen(int sockfd, int backlog);
Listen to Simple understanding of Sockets
sockfd: the Simple understanding of Sockets descriptor to be monitored
backlog: the maximum number of connections that can be queued for the corresponding Simple understanding of Sockets
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Connect to a Simple understanding of Sockets
sockfd: client’s Simple understanding of Sockets descriptor
addr: server’s Simple understanding of Sockets address
addrlen: length of Simple understanding of Sockets address
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
After the TCP server monitors the client request, it calls the accept() function to receive the request
sockfd: the server’s Simple understanding of Sockets descriptor
addr: the client’s Simple understanding of Sockets address
addrlen:Simple understanding of Sockets The length of the address
ssize_t read(int fd, void *buf, size_t count);
Read the Simple understanding of Sockets content
fd: Simple understanding of Sockets descriptor
buf: buffer
count: buffer length
ssize_t write (int fd, const void *buf, size_t count);
Writing content to the Simple understanding of Sockets is actually sending content
fd: Simple understanding of Sockets description word
buf: buffer
count: buffer length
int close (int fd);
Simple understanding of Sockets is marked as closed, so that the reference count of the corresponding Simple understanding of Sockets descriptor is -1. When the reference count is 0, the TCP client is triggered to send a termination request to the server.
Reference
Linux Socket Programming (not limited to Linux)
Uncovering Socket Programming