php can implement socket communication. The implementation method is: 1. Create "socketsocket_create();" on the client and request a connection to the server "socket_connect();"; 2. Create "socketsocket_create()" on the server ;" and bind the IP and port number "socket_bind();"; 3. Read the client's message or write a message to the client through "socket_wirte();socket_read".
The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer
Can php achieve socket communication?
socket PHP: Detailed and simple socket TCP communication PHP implementation
1 Background introduction
Goal: I hope to use socket The function of this server is to accept connections from multiple clients and complete their mutual communication. For example, client A and client B are connected to server S at the same time. Client A sends a message to server S. Server S will forward A's message to B. Similarly, B's message can also be forwarded to A through S. This realizes mutual communication between Client A and Client B. This time we only implement the connection and communication between the client and the server, and do not have the forwarding function of the server for the time being.
2 A brief introduction to TCP
2.1 TCP’s “three-way handshake”
TCP’s three-way handshake can ensure that the server and Clients can communicate normally. Three-way handshake process: As for the explanation of this process, you can click on the link below to view it.
The server needs to have one When communicating with IP or TCP, you also need to provide a port number.
The customer service side needs to have an IP, and when communicating with TCP, it also needs to provide a port number.
Personal understanding: IP is an address, which can be understood as a house. When you need to establish communication, you must first know where the house is. Otherwise, where should we send the information?
PORT port, the port number can be understood as a door in this house. We need to specify a door to send messages in or receive messages from this door.
The server creates a socket, binds IP and PORT, and then enters the listening state to wait for the client to initiate a connection.
#The client creates a socket and connects to the specified server IP and PORT.
The server accepts the connection initiated by the client.
Both parties can send data to each other
Both parties close the socket.
The above is the detailed content of Can php implement socket communication?. For more information, please follow other related articles on the PHP Chinese website!
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn