Simple understanding of Sockets
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



This article brings you relevant knowledge about php+socket, which mainly introduces IO multiplexing and how php+socket implements web server? Friends who are interested can take a look below. I hope it will be helpful to everyone.

1. Socket programming based on TCP protocol 1. The socket workflow starts with the server side. 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, the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection. An interaction ends. Use the following Python code to implement it: importso

The first step on the SpringBoot side is to introduce dependencies. First we need to introduce the dependencies required for WebSocket, as well as the dependencies for processing the output format com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket. The second step is to create the WebSocket configuration class importorg. springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

Solution to the problem that the php socket cannot be connected: 1. Check whether the socket extension is enabled in php; 2. Open the php.ini file and check whether "php_sockets.dll" is loaded; 3. Uncomment "php_sockets.dll".

Common network communication and security problems and solutions in C# In today's Internet era, network communication has become an indispensable part of software development. In C#, we usually encounter some network communication problems, such as data transmission security, network connection stability, etc. This article will discuss in detail common network communication and security issues in C# and provide corresponding solutions and code examples. 1. Network communication problems Network connection interruption: During the network communication process, the network connection may be interrupted, which may cause

With the development of the Internet, file transfer has become an indispensable part of people's daily work and entertainment. However, traditional file transfer methods such as email attachments or file sharing websites have certain limitations and cannot meet the needs of real-time and security. Therefore, using PHP and Socket technology to achieve real-time file transfer has become a new solution. This article will introduce the technical principles, advantages and application scenarios of using PHP and Socket technology to achieve real-time file transfer, and demonstrate the implementation method of this technology through specific cases. technology

This article brings you relevant knowledge about php+socket. It mainly introduces what is socket? How does php+socket realize client-server data transmission? Friends who are interested can take a look below. I hope it will be helpful to everyone.

PHP is a commonly used development language that can be used to develop various web applications. In addition to common HTTP requests and responses, PHP also supports network communication through Sockets to achieve more flexible and efficient data interaction. This article will introduce the methods and techniques of how to implement Socket communication in PHP, and attach specific code examples. What is Socket Communication Socket is a method of communication in a network that can transfer data between different computers. by S
