Phpsocket client and server application instances. Some may be confused about the socket application of php. in this article, I will explain the instance code. I hope to help my new php beginners with the following code: 1. on the server side, there are often friends who are confused about the php socket application. This article will explain the instance code, hoping to help new php users.
The code is as follows:
1. server code:
<? Phpclass SocketServer {private $ _ port = '000000'; private $ _ address = '2017. 0.0.1 '; private $ _ client_socket_list = array (); public function _ set ($ name, $ val) {$ this ---> $ name = $ val ;} private function _ showError ($ error) {exit ($ error);}/*** start the socket server listening port */public function start () {// create a 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
2. client code:
<? Phpclass SocketServer {private $ _ port = '000000'; private $ _ address = '2017. 0.0.1 '; private $ _ client_socket_list = array (); public function _ set ($ name, $ val) {$ this ---> $ name = $ val ;} private function _ showError ($ error) {exit ($ error);}/*** start the socket server listening port */public function start () {// create a 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 in CLI mode on the server. cgi mode times out. this is a common mistake for new users. So what is the CLI mode? To put it simply, run the command line instead of using the browser. Otherwise, it will time out!
The specific code of callback is as follows: 1. server side...