PHP socket client and server-side application example _php tips

Source: Internet
Author: User
Tags php class

Often have friends with PHP socket application is full of doubt, this article on the example code to make a explanation, hope to beginners PHP friends play a little help role

The specific code is as follows:

1. Server-side code:

<?php class 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 socket server-side listening port/Public function start () {//Create port if ($sock = Socket_create (Af_inet, SOC K_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 ("so Cket_bind () Failed:reason: ".
    Socket_strerror (Socket_last_error ($sock))); }//Listen for 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 new users to the client array $this->_client_socket_list[]= $client _socket;
        echo "New connection:\r\n";//server-side output the number of clients currently being 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 gives you a reference to determine if a client has voluntarily lost the connection else{foreach ($this->_client_socket_list as $socket) { $len = Socket_recv ($socket, $buffer, 2048, 0);
    Accept the client information, if 0 represents a disconnect if ($len < 7) {//Here is to go to connect the client Business}}} *  
  }while (TRUE); /** * Send data to client/Public function send ($client _socket, $str) {return socket_write ($client _socket, $STR, S
  Trlen ($STR)); /** * Accept data from client/Public function read ($client _socket) {return Socket_read ($client _socket, 8192);//8192 the accepted length of the actual representation, I use 819292 to express a bit longer, so that a longer string can also be accepted, less than 8192 does not matter, will automatically identify} $socket _
Server =new socketserver ();

 $socket _server->start ()/Start listening

2. Client code:

<?php class 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 socket server-side listening port/Public function start () {//Create port if ($sock = Socket_create (Af_inet, SOC K_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 ("so Cket_bind () Failed:reason: ".
    Socket_strerror (Socket_last_error ($sock))); }//Listen for 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 new users to the client array $this->_client_socket_list[]= $client _socket;
        echo "New connection:\r\n";//server-side output the number of clients currently being 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 gives you a reference to determine if a client has voluntarily lost the connection else{foreach ($this->_client_socket_list as $socket) { $len = Socket_recv ($socket, $buffer, 2048, 0);
    Accept the client information, if 0 represents a disconnect if ($len < 7) {//Here is to go to connect the client Business}}} *  
  }while (TRUE); /** * Send data to client/Public function send ($client _socket, $str) {return socket_write ($client _socket, $STR, S
  Trlen ($STR)); /** * Accept data from client/Public function read ($client _socket) {return Socket_read ($client _socket, 8192);//8192 the accepted length of the actual representation, I use 819292 to express a bit longer, so that a longer string can also be accepted, less than 8192 does not matter, will automatically identify} $socket _
Server =new socketserver (); $socket _server->start ()/Start listening

Note: server-side please run in CLI mode, the CGI mode will time out, this is a novice often like to make mistakes. So what is the CLI mode? The simple way is to use the command line to execute, and do not use the browser to 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.