A cainiao friend suddenly asked me this question... Now, I wrote a simple example for him, and posted it by the way.
Server:
$ Name = $ val;} private function _ showError ($ error) {exit ($ error );} /*** start the socket server listening port */public function start () {// create the port if ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP )) === false) {$ this-> _ showError ("socket_create () failed: reason :". socket_strerror (socket_last_error ();} // bind if (socket_bind ($ sock, $ this-> _ address, $ this-> _ port) = false) {$ this-> _ showError ("socket_bind () failed: reason :". socket_strerror (socket_last_error ($ sock);} // listen to if (socket_listen ($ sock, 5) === false) {$ this-> _ showError ("socket_bind () failed: reason :". socket_strerror (socket_last_error ($ sock);} do {// if ($ client_socket = socket_accept ($ sock) when a client is connected )) {$ count = count ($ this-> _ client_socket_list) + 1; // Add new users to the client array $ this-> _ client_socket_list [] = $ client_socket; echo "new connection: \ r \ n"; // number of clients currently connected to on the server side echo "current connection: {$ count} \ r \ n "; // accept the string sent from the client $ msg = $ this-> read ($ client_socket); echo "client: {$ msg} \ r \ n "; // The server transmits $ my_msg = "I am fine, think you \ r \ n"; $ this-> send ($ client_socket, $ my_msg) to the client );} /*** this code is for your reference to determine whether a client has actively lost the connection to else {foreach ($ this-> _ client_socket_list as $ socket) {$ len = socket_recv ($ socket, $ buffer, 2048, 0); // accept the client information. if the value is 0, the connection is closed if ($ len <7) {// write here as the client service to be connected} */} while (true);}/*** send data to the client */public function send ($ client_socket, $ str) {return socket_write ($ client_socket, $ str, strlen ($ str);}/*** receive data from the client */public function read ($ client_socket) {return socket_read ($ client_socket, 8192); // The accept length actually represented by 8192. I use 819292 to indicate a longer string, so that a longer string can be accepted, it does not matter if it is less than 8192, it will automatically recognize} $ socket_server = new SocketServer (); $ socket_server-> start (); // start listening
Client:
$ Name = $ val;} private function _ showError ($ error) {exit ($ error );} /*** start the socket server listening port */public function start () {// create the port if ($ sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP )) === false) {$ this-> _ showError ("socket_create () failed: reason :". socket_strerror (socket_last_error ();} // bind if (socket_bind ($ sock, $ this-> _ address, $ this-> _ port) = false) {$ this-> _ showError ("socket_bind () failed: reason :". socket_strerror (socket_last_error ($ sock);} // listen to if (socket_listen ($ sock, 5) === false) {$ this-> _ showError ("socket_bind () failed: reason :". socket_strerror (socket_last_error ($ sock);} do {// if ($ client_socket = socket_accept ($ sock) when a client is connected )) {$ count = count ($ this-> _ client_socket_list) + 1; // Add new users to the client array $ this-> _ client_socket_list [] = $ client_socket; echo "new connection: \ r \ n"; // number of clients currently connected to on the server side echo "current connection: {$ count} \ r \ n "; // accept the string sent from the client $ msg = $ this-> read ($ client_socket); echo "client: {$ msg} \ r \ n "; // The server transmits $ my_msg = "I am fine, think you \ r \ n"; $ this-> send ($ client_socket, $ my_msg) to the client );} /*** this code is for your reference to determine whether a client has actively lost the connection to else {foreach ($ this-> _ client_socket_list as $ socket) {$ len = socket_recv ($ socket, $ buffer, 2048, 0); // accept the client information. if the value is 0, the connection is closed if ($ len <7) {// write here as the client service to be connected} */} while (true);}/*** send data to the client */public function send ($ client_socket, $ str) {return socket_write ($ client_socket, $ str, strlen ($ str);}/*** receive data from the client */public function read ($ client_socket) {return socket_read ($ client_socket, 8192); // The accept length actually represented by 8192. I use 819292 to indicate a longer string, so that a longer string can be accepted, it does not matter if it is less than 8192, it will automatically recognize} $ socket_server = new SocketServer (); $ socket_server-> start (); // start listening
Note: run on the server in CLI mode. cgi mode will time out. New users often like to make mistakes. what is the CLI mode, simply put, is to use the command line to execute, instead of using the browser to open, otherwise it will time out