Server File: socket_server.php:
<? PHP
Phpinfo ();
// When you open a web connection, you must set an unlimited number of valid values to prevent timeout connections.
Set_time_limit (0 );
/** Load the required socket library */
If (! Extension_loaded ('buckets ')){
If (strtoupper (substr (php_ OS, 3) = "win "){
DL ('php _ sockets. dll ');
} Else {
DL ('ets ETS. so ');
}
}
/** --- Socket server generation ---*/
$ Commonprotocol = getprotobyname ("TCP"); // use a public protocol name to obtain a protocol type.
$ Socket = socket_create (af_inet, sock_stream, $ commonprotocol); // an instance that generates a socket and returns a socket Resource
Socket_bind ($ socket, 'localhost', 1339); // bind the socket to the Local Computer
Socket_listen ($ socket); // listen to all incoming socket connections
/** ------ End ------*/
// Initialize the buffer
$ Buffer = 'no data ';
While (true ){
// Generate a connection
$ Connection = socket_accept ($ socket); // It is actually a process of loop waiting.
If ($ connection ){
Printf ("Socket connected \ r \ n ");
Socket_write ($ connection, "You are connecting me, my load .");
} Else {
Echo socket_strerror ();
Exit;
}
// Detect the content of the buffer zone. If there is any content, it will be sent. If there is no content, it will not be sent.
If ($ buffer! = ""){
Printf ("something is in the buffer... sending data... \ r \ n ");
Socket_write ($ connection, $ buffer. ";"); // send the buffer content to the client
Printf ("wrote to socket \ r \ n ");
} Else
{
Printf ("no data in the buffer \ r \ n ");
}
// Obtain the input data from the client and put the read data into the buffer zone
While ($ DATA = socket_read ($ connection, 1024) // socket_read () reads data of the specified length
{
$ Buffer = $ data;
Socket_write ($ connection, "information stored ed \ r \ n ");
Printf ("buffer". $ buffer. "\ r \ n ");
}
// Close the socket connection
Socket_close ($ connection );
Printf ("closed the soket \ r \ n ");
}
?>
Client File: socket_client.php:
<? PHP
// Create the socket and connect
$ Socket = socket_create (af_inet, sock_stream, sol_tcp );
$ Connection = socket_connect ($ socket, 'localhost', 1339 );
While ($ buffer = socket_read ($ socket, 10, php_binary_read ))! = False ){
If ($ buffer = "no data "){
Echo ("<p> no data </P> ");
Break;
} Else {
// Do something with the data in the buffer
Echo ("<p> buffer data:". $ buffer. "</P> ");
}
}
Echo ("<p> writing to socket </P> ");
// Write some test data to our socket
If (! Socket_write ($ socket, "some data ")){
Echo ("<p> write failed </P> ");
}
// Read any response from the socket
While ($ buffer = socket_read ($ socket, 10, php_binary_read ))! = False ){
Echo ("<p> data sent was: Some data <br> response was:". $ buffer. "</P> ");
}
Echo ("<p> done reading from socket </P> ");
?>
Then run php.exe in the command line:
The client cannot normally display all information that should be displayed:
"<P> done reading from socket </P>" the last sentence is not displayed.
It seems that the first loop is stuck on the socket_read () function. When there is no data on the server side, it was originally thought that it would return false, but there was no reflection at all, the program does not go down, as if there is an infinite loop in socket_read. What's wrong! I have referred to the PHP document, and some say "" is returned. Some example programs say "false". There is no way to ask for help. What is the above phenomenon?
My environment is Windows XP, Apache, PhP5