How to implement simple php socket communication

伊谢尔伦
Release: 2023-03-07 20:32:01
Original
22988 people have browsed it

How to implement simple php socket communication

socket, also commonly known as "socket", is used to describe the IP address and port, and is the handle of a communication chain. Applications usually make requests to the network or respond to network requests through "sockets".Socket is neither a program nor a protocol. It is just a set of abstract APIs for the communication layer provided by the operating system. The previous chapter introduced some common and important functions ofphp socket, which will be used in socket communication.

Communication requires server and client components:

Server side:Use php to initialize the socket and then bind a port to monitor the port . Call accept to block and wait for the client to connect.

Client:The client initializes a socket and then connects to the server. 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, and the interaction ends.

Client-server are applications that can interact with each other. The interaction between client and server requires a connection. Socket programming is responsible for establishing interactive connections between applications.

Socket connection process

Depending on how the connection is initiated and the target to which the local socket is connected, the connection process between sockets can be divided into three steps. : Server monitoring, client request, connection confirmation.

(1) Server monitoring: The server-side socket does not locate the specific client socket, but is in a state of waiting for connection and monitors the network status in real time.

(2) Client request: refers to a connection request made by the client's socket, and the target to be connected is the server's socket. To do this, the client's socket must first describe the server's socket to which it wants to connect, indicate the address and port number of the server-side socket, and then make a connection request to the server-side socket.

(3) Connection confirmation: means that when the server-side socket listens or receives the connection request of the client socket, it responds to the request of the client socket and establishes a new Thread, sends the description of the server-side socket to the client. Once the client confirms this description, the connection is established. The server-side socket continues to be in the listening state and continues to receive connection requests from other client sockets.

Socket principle can refer to the flow chart below:

How to implement simple php socket communication

The following is a simple implementation of socket communication through a server-client code example. The whole process

1. Its server code:

= 5){ break; }; } //echo $buf; socket_close($msgsock); } while (true); socket_close($sock); ?>
Copy after login

Run the php file,After running, the result should not be visible, you can usenetstat -ntlp Check whether port 8001 is occupied. See picture below.

How to implement simple php socket communication

2. Its client code:

$in 
"; } while($out = socket_read($socket, 8192)) { echo "接收服务器回传信息成功!\n"; echo "接受的内容为:",$out; } echo "关闭SOCKET...\n"; socket_close($socket); echo "关闭OK\n"; ?>
Copy after login

Look at the server window result:

How to implement simple php socket communication

Note: The characteristics of the PHP language determine that in this regard, php is only suitable for the client, not the server.

【Recommended related tutorials】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2. #php programming from entry to master a complete set of tutorials

The above is the detailed content of How to implement simple php socket communication. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!