PHP code for implementing the Socket server

Source: Internet
Author: User
: PHP Socket server code: & lt ;? Phpob_implicit_flush (); set_time_limit (0); $ address
 "; If (socket_bind ($ socket, $ address, $ port) = false) echo" error (socket_bind): ". socket_strerror (socket_last_error ())."
"; If (socket_listen ($ socket) = false) echo" error (socket_listen): ". socket_strerror (socket_last_error ())."
";/* After the socket has been created using socket_create () and bound to a name with socket_bind (), it may be told to listen for incoming connections on socket. */while (true) {if ($ msgSocket = socket_accept ($ socket) = false) {echo "error (socket_accept ):". socket_strerror (socket_last_error ())."
"; Break;}/* this function will accept incoming connections on that socket. once a successful connection is made, a new socket resource is returned, which may be used for communication. if there are multiple connections queued on the socket, the first will be used. if there are no pending connections, socket_accept () will block until a connection becomes present. if socket has been made non-block Ing using socket_set_blocking () or socket_set_nonblock (), FALSE will be returned. */$ msg = "Welcome!
"; // Socket_write ($ msg, $ msg, strlen ($ msg); $ command =" "; while (true) {if ($ buf = socket_read ($ msgSocket, 2048, PHP_BINARY_READ) = false) {echo "error (socket_read ):". socket_strerror (socket_last_error ())."
"; Break 2;}/* The function socket_read () reads from the socket resource socket created by the socket_create () or socket_accept () functions. the maximum number of bytes read is specified by the length parameter. otherwise you can use \ r, \ n, or \ 0 to end reading (depending on the type parameter, see below ). * // * if (! $ Buf = trim ($ buf) continue ;//???? If ($ buf = "quit") break; if ($ buf = "shutdown") {socket_close ($ msgSocket); break 2;} $ tallBack = "You say: $ buf \ n "; socket_write ($ msgSocket, $ tallBack, strlen ($ tallBack); */if (ord ($ buf )! = 13) $ command. = $ buf; else {$ command1 = "You Say: $ command \ r \ n"; socket_write ($ msgSocket, $ command1, strlen ($ command1 )); echo "User typed :". $ command."
"; $ Command =" ";}} socket_close ($ msgSocket);} socket_close ($ socket);?>

Open CMD and enter "telnet 192.40.7.93 10000". try it on your own!

The above describes the PHP code for implementing the Socket server, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.