PHP listening Socket. Read the PHP listening Socket ,? Phpset_time_limit (10); $ commonProtocol = getprotobyname ("> <LINKhref =" http://www.php100.com//statics/style/headfloor_950_081205.css"
Set_time_limit (10 );
$ CommonProtocol = getprotobyname ("tcp ");
$ Socket = socket_create (AF_INET, SOCK_STREAM, $ commonProtocol );
If ($ socket ){
$ Result = socket_bind ($ socket, 'localhost', 1337 );
If ($ result ){
$ Result = socket_listen ($ socket, 5 );
If ($ result ){
Echo "listening successful ";
}
}
} Else {
Echo "failed to listen ";
}
Do {
If ($ msgsock = socket_accept ($ socket) {/* send a prompt to the connected user */
$ Msg = "======================================== =======\ r \ n ".
"Welcome to the PHP Test Server. \ r \ n ".
"To quit, type 'quit'. \ r \ n ".
"To shut down the server type 'shutdown '. \ r \ n ".
"To get help message type 'HELP'. \ r \ n ".
"============================================== ===\ R \ n ".
"Php> ";
}
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Do {
$ Buf = socket_read ($ msgsock, 2048, PHP_BINARY_READ );
If (false ===$ buf ){
Echo "socket_read () failed: reason:". socket_strerror ($ result). "\ n ";
Break 2;
}
If (! $ Buf = trim ($ buf )){
Continue;
}/* Close the client connection when the client enters the quit command */
If ($ buf = 'Q '){
Break;
}/* When the client enters the shutdown command, both the server and client are closed */
If ($ buf = 'shutdown '){
Socket_close ($ msgsock );
Break 2;
}/* The client outputs help information when entering the help command */
If ($ buf = 'h '){
$ Msg = "PHP Server Help Message \ r \ n ".
"To quit, type 'quit'. \ r \ n ".
"To shut down the server type 'shutdown '. \ r \ n ".
"To get help message type 'HELP'. \ r \ n ".
"Php> ";
Socket_write ($ msgsock, $ msg, strlen ($ msg ));
Continue;
}/* Message displayed when the command entered by the client does not exist */
$ Talkback = "PHP: unknow command '$ buf'. \ r \ nphp> ";
Socket_write ($ msgsock, $ talkback, strlen ($ talkback ));
Echo "$ buf \ n ";
} While (true );
Socket_close ($ msgsock );
} While (true );
/* Close the Socket connection */
Socket_close ($ socket );
?>