Home>Article>Backend Development> How to connect socket socket in PHP
Socket is usually also called "socket", which is used to describe the IP address and port. It is the handle of a communication chain. Applications usually make requests to the server or respond to network requests through "sockets". This article mainly talks about the connection process between socket characters in PHP, which has certain reference value. Interested friends can learn about it.
Depending on how the link is started and the target to which the local socket is connected, the connection process between sockets can be divided into three steps:
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 familiarize itself with the server's socket to which it wants to connect, indicate the address and port number of the server's socket, and then make a connection request just like the server's 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. , create a new thread and send 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 link requests from other client sockets.
1. Test environment:
Server ip: xxx.xxx.xxx.1
# # Client ip: xxx.xxx.xxx.2
2. Test process:
The client server will be on this machine (xxx .xxx.xxx.1) Send data to the socket server of the server (xxx.xxx.xxx.2) through socket. After receiving the data sent by the client, the server returns some data to the client.
3. Server file content:
4. Client file content:
5. Start the process on the server socket service.
#/usr/local/php/bin/php -a/home/server.php Interactive mode enable 互动模式启动
6. After the server is started, check the started process and port
#netstat -tnlp |grep 10008 tcp 0 0 192.168.13:10008 0.0.0.0:* LISTEN 28892/php
7. Perform transmission on the client (192.168.1.2)
#/usr/local/php/bin/php -a client.php Interactive mode enabled
8. Return to the server to view acceptance information arrived.
#/usr/local/php/bin/php -a /home/server.php Interactive mode enabled Socket connected send data client Wrote to socket Buffer:This data will Send to server!Related tutorials:
The above is the detailed content of How to connect socket socket in PHP. For more information, please follow other related articles on the PHP Chinese website!