PHP socket client and server-side application examples

Source: Internet
Author: User
This article mainly introduces the PHP socket client and server-side application example, the need for friends can refer to the following

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:

<?phpclass socketserver{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); }/** * Starts the socket server-side listening port */Public Function start () {//Create port if ($sock = Socket_create (Af_inet, Sock_stre AM, 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 ("Socke T_bind () Failed:reason: ".    Socket_strerror (Socket_last_error ($sock))); }//Listen 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 passed by the client $msg = $this->read ($client _socket);        echo "client:{$msg}\r\n";        The server passes the value to the 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 the client Business}}} */}whi    Le (True);  /** * Send data to client */Public function Send ($client _socket, $str) {return socket_write ($client _socket, $str, strlen  ($STR)); }/** * accepts data from client */Public function read ($client _socket) {return Socket_read ($client _socket, 8192);//8192 actually represents the Accept the length, I use 819292 to indicate a bit longer,Such a long string can also be accepted, less than 8192 does not matter, will be automatically recognized}} $socket _server =new socketserver (); $socket _server->start ();//Start monitoring 

2. Client code:

<?phpclass socketserver{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); }/** * Starts the socket server-side listening port */Public Function start () {//Create port if ($sock = Socket_create (Af_inet, Sock_stre AM, 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 ("Socke T_bind () Failed:reason: ".    Socket_strerror (Socket_last_error ($sock))); }//Listen 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 passed by the client $msg = $this->read ($client _socket);        echo "client:{$msg}\r\n";        The server passes the value to the 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 the client Business}}} */}whi    Le (True);  /** * Send data to client */Public function Send ($client _socket, $str) {return socket_write ($client _socket, $str, strlen  ($STR)); }/** * accepts data from client */Public function read ($client _socket) {return Socket_read ($client _socket, 8192);//8192 actually represents the Accept the length, I use 819292 to indicate a bit longer,Such a long string can also be accepted, less than 8192 does not matter, will be automatically recognized}} $socket _server =new socketserver (); $socket _server->start ();//Start monitoring 

Note: server-side run in CLI mode, 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!

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.