PHP Socket Programming Process detailed
Introduced
Sockets are used for interprocess communication. Interprocess communication is typically based on a client-server model. At this point, the client-server is an application that can interact with each other. The interaction between the client and the server requires a connection. Socket programming is responsible for establishing an interactive connection between applications.
In this article, we'll learn how to create a simple client-server with PHP. We will also learn how client applications can send messages to the server and how to accept messages from the server.
Using code
Purpose: To develop a client to send a string message to the server, and the service side reverses the same information back to the client.
PHP Server
1th Step: Set variables, such as "host" and "Port"
$host = "127.0.0.1";
$port = 5353;
No Timeout
Set_time_limit (0);
The port number can be any positive integer between 1024-65535.
2nd step: Create a socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("Could not create socket\n");
3rd step: Bind socket to port and host
The socket resource created is bound to the IP address and port number.
$result = Socket_bind ($socket, $host, $port) or Die ("Could not bind to socket\n");
4th step: Start the socket monitor
After binding to IP and ports, the server begins to wait for the client to connect. It waits until the connection is not connected.
$result = Socket_listen ($socket, 3) or Die ("Could not set up socket listener\n");
5th step: Accept the connection
This function will accept incoming connection requests from the built socket. After accepting the connection from the client socket, the function returns another socket resource, which is actually responsible for communicating with the corresponding client socket. The "$spawn" here is the socket resource responsible for communicating with the client socket.
$spawn = socket_accept ($socket) or Die ("Could not accept incoming connection\n");
So far, we've got the server socket ready, but in fact the script doesn't do anything. So in order to continue with this goal, we will read the client socket message and then reverse the received message and send it back to the client socket.
6th step: Read the message from the client socket
$input = Socket_read ($spawn, 1024x768) or Die ("Could not read input\n");
7th step: Reverse the message
$output = Strrev ($input). "\ n";
8th step: Send a message to the client socket
Socket_write ($spawn, $output, strlen ($output)) or Die ("Could not write output\n");
Close socket
Socket_close ($spawn);
Socket_close ($socket);
This completes the service side. Now, let's learn how to create a PHP client.
PHP Client
The first two steps are the same as the service side.
1th Step: Set variables, such as "host" and "Port"
$host = "127.0.0.1";
$port = 5353;
No Timeout
Set_time_limit (0);
Note: The ports and hosts here should be the same as those defined in the server.
2nd step: Create a socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("Could not create socket\n");
3rd step: Connect to the server
$result = Socket_connect ($socket, $host, $port) or Die ("Could not connect toserver\n");
At this point, unlike the server, the client socket is not bound to the port and host. Instead, it connects to the server socket and waits for the connection from the client socket to be accepted. This step establishes the client socket connection to the server socket.
4th step: Write to the server socket
Socket_write ($socket, $message, strlen ($message)) or Die ("Could not send data to server\n");
In this step, the client socket data is sent to the server socket.
5th step: Read the response from the server
$result = Socket_read ($socket, 1024x768) or Die ("Could not read server response\n");
echo "Reply from Server:". $result;
6th step: Close the socket
Socket_close ($socket);
The complete code
Service-side server.php)
Set some variables
$host = "127.0.0.1";
$port = 25003;
Don ' t timeout!
Set_time_limit (0);
Create socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("Could not create socket\n");
Bind socket to Port
$result = Socket_bind ($socket, $host, $port) or Die ("Could not bind to socket\n");
Start listening for connections
$result = Socket_listen ($socket, 3) or Die ("Could not set up socket listener\n");
Accept Incoming Connections
Spawn another socket to handle communication
$spawn = socket_accept ($socket) or Die ("Could not accept incoming connection\n");
Read Client input
$input = Socket_read ($spawn, 1024x768) or Die ("Could not read input\n");
Clean up input string
$input = Trim ($input);
echo "Client Message:" $input;
Reverse client input and send back
$output = Strrev ($input). "\ n";
Socket_write ($spawn, $output, strlen ($output)) or Die ("Could not write output\n");
Close sockets
Socket_close ($spawn);
Socket_close ($socket);
Client client.php)
$host = "127.0.0.1";
$port = 25003;
$message = "Hello Server";
echo "Message to Server:". $message;
Create socket
$socket = Socket_create (af_inet, sock_stream, 0) or die ("Could not create socket\n");
Connect to Server
$result = Socket_connect ($socket, $host, $port) or Die ("Could does connect to server\n");
Send string to Server
Socket_write ($socket, $message, strlen ($message)) or Die ("Could not send data to server\n");
Get server response
$result = Socket_read ($socket, 1024x768) or Die ("Could not read server response\n");
echo "Reply from Server:". $result;
Close socket
Socket_close ($socket);
After establishing the above files server.php and client.php), do the following:
License
This article, along with any related source code and files, is licensed through the Code Project Open License (Cpol).
http://www.bkjia.com/PHPjc/1040141.html www.bkjia.com true http://www.bkjia.com/PHPjc/1040141.html techarticle The PHP socket programming process provides a detailed description of the socket for interprocess communication. Interprocess communication is typically based on the client server-side model. At this point, the client server is an application that can interact with each other ...