PHP listening Socket

Source: Internet
Author: User
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 );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.