Often friends will be full of doubts about the application of the PHP socket, this article is an example of the code to explain, I hope to beginners PHP friends play a little help role
The specific code is as follows:
1. Server-side code:
<?PHPclasssocketserver{Private $_port= ' 9000 '; Private $_address= ' 127.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-side listening port*/ Public functionstart () {//Creating Ports if(($sock= Socket_create (Af_inet, Sock_stream, sol_tcp)) = = =false) { $this->_showerror ("Socket_create () Failed:reason:".Socket_strerror (Socket_last_error ())); } //binding if(Socket_bind ($sock,$this->_address,$this->_port) = = =false) { $this->_showerror ("Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock ) )); } //Monitor if(Socket_listen ($sock, 5) = = =false) { $this->_showerror ("Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock ) ) ); } Do { //when there is a client connection if($client _socket=socket_accept ($sock )) { $count=Count($this->_client_socket_list) + 1; //Add the new user to the client array $this->_client_socket_list[]=$client _socket; Echo"New connection:\r\n";//server-side output the number of clients currently connected Echo"Current connection:{$count}\r\n "; //accept the string that the client passed over $msg=$this->read ($client _socket); Echo"client:{$msg}\r\n "; //server passes value to client $my _msg= "I am fine,think you\r\n"; $this->send ($client _socket,$my _msg); } /** This code is for your reference to determine if there is a client actively loses connection else{foreach ($this->_client_socket_list as $socket) { $len = Socket_recv ($socket, $buffer, 2048, 0); Accept the client information if the 0 delegate disconnects if ($len < 7) {//write here to connect to the client business} }}*/ } while(true); } /** * Send data to client*/ Public functionSend$client _socket,$str){ returnSocket_write ($client _socket,$str,strlen($str ) ); } /** * Accept data from client*/ Public functionRead$client _socket){ returnSocket_read ($client _socket, 8192);//8192 The actual representation of the accepted length, I use 819292 to indicate a bit longer, so that a longer string can also be accepted, less than 8192 is OK, will automatically identify }}$socket _server=Newsocketserver ();$socket _server->start ();//Start listening .
2. Client code:
<?PHPclasssocketserver{Private $_port= ' 9000 '; Private $_address= ' 127.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-side listening port*/ Public functionstart () {//Creating Ports if(($sock= Socket_create (Af_inet, Sock_stream, sol_tcp)) = = =false) { $this->_showerror ("Socket_create () Failed:reason:".Socket_strerror (Socket_last_error ())); } //binding if(Socket_bind ($sock,$this->_address,$this->_port) = = =false) { $this->_showerror ("Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock ) )); } //Monitor if(Socket_listen ($sock, 5) = = =false) { $this->_showerror ("Socket_bind () Failed:reason:". Socket_strerror (Socket_last_error ($sock ) ) ); } Do { //when there is a client connection if($client _socket=socket_accept ($sock )) { $count=Count($this->_client_socket_list) + 1; //Add the new user to the client array $this->_client_socket_list[]=$client _socket; Echo"New connection:\r\n";//server-side output the number of clients currently connected Echo"Current connection:{$count}\r\n "; //accept the string that the client passed over $msg=$this->read ($client _socket); Echo"client:{$msg}\r\n "; //server passes value to client $my _msg= "I am fine,think you\r\n"; $this->send ($client _socket,$my _msg); } /** This code is for your reference to determine if there is a client actively loses connection else{foreach ($this->_client_socket_list as $socket) { $len = Socket_recv ($socket, $buffer, 2048, 0); Accept the client information if the 0 delegate disconnects if ($len < 7) {//write here to connect to the client business} }}*/ } while(true); } /** * Send data to client*/ Public functionSend$client _socket,$str){ returnSocket_write ($client _socket,$str,strlen($str ) ); } /** * Accept data from client*/ Public functionRead$client _socket){ returnSocket_read ($client _socket, 8192);//8192 The actual representation of the accepted length, I use 819292 to indicate a bit longer, so that a longer string can also be accepted, less than 8192 is OK, will automatically identify }}$socket _server=Newsocketserver ();$socket _server->start ();//Start listening .
Note: Server side use CLI mode to run, CGI mode will time out, this is the novice often like to make mistakes. So what is the CLI mode? Simple is to use the command line to execute, and do not use the browser open, otherwise it will time out!
PHP socket client and server-side application Example