PHP implementation of the socket server code

Source: Internet
Author: User
<?php Ob_implicit_flush (); Set_time_limit (0); $address = "192.40.7.93";//change to your own address $port = 10000; if ($socket = Socket_create (af_inet,sock_stream,sol_tcp)) = = False) echo "Error (Socket_create):". Socket_strerror (socket _last_error ()). " <br/> "; if (Socket_bind ($socket, $address, $port) = = False) echo "Error (Socket_bind):". Socket_strerror (Socket_last_error ()). " <br/> "; if (Socket_listen ($socket) = = False) echo "Error (Socket_listen):". Socket_strerror (Socket_last_error ()). " <br/> "; /* After the socket sockets have been created using socket_create () and bound to a name with Socket_bind (), it could be told T o Listen for incoming connections on socket. */while (true) {if ($msgSocket = socket_accept ($socket)) = = False) {echo "error (Socket_accept):". Socket_strerror (socket_ Last_error ()). " <br/> "; Break }/* This function would accept incoming connections on the socket. Once A successful connection is made, a new socket resource are returned, which may are used for communication. If there isMultiple connections queued on the socket, the first would be used. If there is no pending connections, Socket_accept () would block until a connection becomes present. If socket has been made non-blocking using socket_set_blocking () or Socket_set_nonblock (), FALSE would be returned. */$msg = "welcome!<br/>"; 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 ()). " <br/> "; Break 2; }/* The function Socket_read () reads from the socket resource sockets created by the Socket_create () or socket_accept () Fu Nctions. The maximum number of bytes read is specified by the length parameter. Otherwise you can use \ r, \ n, or 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 = "say: $buf \ n"; Socket_write ($msgSocket, $tallBack, strlen ($tallBack)); */if (ORD ($buf)! =) $command. = $buf; else{$command 1 = "you Say: $command \ r \ n"; Socket_write ($msgSocket, $command 1,strlen ($command 1)); echo "User typed:". $ command. " <br/> "; $command = "";}} Socket_close ($msgSocket); } socket_close ($socket);?>

Then open cmd, enter: Telnet 192.40.7.93 10000, experience it yourself!

Note, to put: Php_sockets.dll Open

The above describes the PHP implementation of the socket server code, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.