Regarding the socket of php, I recently learned the socket of php and want to test my understanding of socket through a function implementation: 1. the client sends data to the server. 2. the server receives the data sent by the client, A response is sent to the client. 3. the client displays the response information sent from the server. The following is the code server's php socket.
I recently learned php socket and want to test my understanding of socket through a function implementation:
1. the client sends data to the server
2. the server receives the data sent by the client and sends a response to the client.
3. the client displays the response information sent from the server.
The following code is used:
Server. php:
// Set up our socket
$ CommonProtocol = getprotobyname ("tcp ");
$ Socket = socket_create (AF_INET, SOCK_STREAM, $ commonProtocol );
Socket_bind ($ socket, 'localhost', 1234 );
Socket_listen ($ socket );
// Initialize the buffer
$ Buffer = "no data ";
While (true ){
// Accept any connections coming in on this socket
$ Connection = socket_accept ($ socket );
Printf ("Socket connected \ r \ n ");
// Check to see if there is anything in the buffer
$ Buffer = socket_read ($ connection, 2048, PHP_NORMAL_READ );
Echo "recived from client". $ buffer;
$ ResponseToClient = $ buffer. "is handled by server! \ N ";
Socket_write ($ connection, $ ResponseToClient );
Echo "response to client! \ N ";
}
?>
Client. php:
// Create the socket and connect
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP );
$ Connection = socket_connect ($ socket, 'localhost', 65500 );
$ I = 0;
$ S = 0;
While (true)
{
Echo "sending data". $ I ++. "\ n ";
$ SendDataToServer = socket_write ($ socket, "server". $ s ++ );
$ ResponseFromServer = socket_read ($ sockets, 2048, PHP_NORMAL_READ );
Echo "received". $ ResponseFromServer. "from server \ n ";
}
?>
Problem: the client only displays sending data0
The server only displays Socket connected
Nothing else is displayed. if the client is not able to send data continuously, the server continuously processes the data and returns it to the client, please kindly advise and thank you very much for sharing with socket PHP:
------ Solution --------------------
Why is there no need to do this?
However, we can give you a test example written in early years.
Server
// Server
// Set error handling
Error_reporting (E_ALL );
// Set the running time
Set_time_limit (0 );
// Starting buffer
Ob_implicit_flush ();
$ Ip = "127.0.0.1"; // ip address
$ Port = 1000; // port number
$ Socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Create a SOCKET
If ($ socket)
Echo "socket_create () successed! \ N ";
Else
Echo "socket_create () failed:". socket_strerror ($ socket). "\ n ";
$ Bind = socket_bind ($ socket, $ ip, $ port); // bind a SOCKET
If ($ bind)
Echo "socket_bind () successed! \ N ";
Else
Echo "socket_bind () failed:". socket_strerror ($ bind). "\ n ";
$ Listen = socket_listen ($ socket); // interlistener SOCKET
If ($ listen)