PHP Socket programming Read completion after writing blocking

Source: Internet
Author: User
Tags php server
Socket TCP PHP Server

The server-side code is as follows:
   

Client side, the code is as follows:
    
If the server side is replaced with the client read operation as a loop internal read, the client will remain in the wait data state.
Is there a way to let the server loop read the case, after reading to the client to write data?




Reply to discussion (solution)

You have nothing wrong with this loop, but once the data is sent, the socket connection is closed and there is no need to loop.

    

     

You have nothing wrong with this loop, but once the data is sent, the socket connection is closed and there is no need to loop.
This is only a demo, the actual situation, the client sent over the data is not long, so can only loop receive

       
In the reading loop, you really shouldn't use the php_normal_read option.
Removing php_normal_read This option does not solve the problem, is there a workaround?

while (true) {    $msg = socket_accept ($socket);               Accept a socket    if (! $msg) {        echo "socket_accept () failed:". Socket_strerror ($msg). " \ n ";        break;    }    while (true) {        $command = Strtoupper (Trim (Socket_read ($msg, 1024)));//wait for client data        if (! $command) break;        Related processing, such as direct echo        socket_write ($msg, $command, strlen ($command));    }    Socket_close ($msg);    if ($command = = "QUIT")//If you receive an end of communication break        ;} Socket_close ($socket);                   Close socket


        
In the reading loop, you really shouldn't use the php_normal_read option.
Removing php_normal_read This option does not solve the problem, is there a workaround?
I do it all with a stream function.
       ip = $ip;        $this->port = $port;        Self::init ();        $this->createserver ();        $this->log (' listenning user ... ');    $this->listenninguser ();        } protected function Createserver () {$errno;        $ERRSTR; $this->websocket = Stream_socket_server ("tcp://". $this->ip. ':' .        $this->port, $errno, $ERRSTR);            if (! $this->websocket) {self::log ("$errstr ($errno)");        Exit ();    } $this->log (' Server OK. ');            } protected function Listenninguser () {while (true) {$this->userindex++;            $user = $this->users[$this->userindex] = stream_socket_accept ($this->websocket, 9999999999); if (Is_resource ($this->users[$this->userindex-1])) {$u = $this->users[$this->u                SERINDEX-1];                $u->close ();                $u = NULL; unset ($this->users[$this->userinDex-1]);            }//$this->log (' Connect to New user ');        $this->listenningmessage ();            }} protected function Listenningmessage () {while (Is_resource ($this->users[$this->userindex]) {            $this->message = Stream_socket_recvfrom ($this->users[$this->userindex], 10270000);                if (! $this->message) {$this->closeuser ();            Break        } $this->messageoperate ();        }} function Messageoperate () {$this->log ("received message:");        $this->log ($this->message);    Mb_strstr ($haystack, $needle, $before _needle, $encoding) $this->sendmessage (' done ');        } function SendMessage ($msg) {if ($msg = = =) {return-1;    } return Stream_socket_sendto ($this->users[$this->userindex], $msg); Public Function Closeuser () {if (!is_resource ($this->users[$this->userindex])) return FALSE;        @stream_socket_shutdown ($this->users[$this->userindex], stream_shut_rdwr);        @fclose ($this->users[$this->userindex]);        $this->log ("User connection disconnected.");    return TRUE;        } Public Function shutdown () {Stream_socket_shutdown ($this->websocket, stream_shut_rdwr);    Fclose ($this->websocket);        } protected static function init () {error_reporting (e_all ^ e_notice);        Set_time_limit (0);        Ob_implicit_flush ();        Date_default_timezone_set (' Asia/shanghai ');        Ignore_user_abort (TRUE);    Mb_internal_encoding (' GBK '); } protected static function log ($message) {echo "\ r \ n". $message.    "\ r \ n"; }} $server = new Socketserver ();


       Init ();        $this->client = stream_socket_client ("tcp://$domain: $port", $errno, $errstr, 300000);            if (! $this->client) {$this->log ("$errstr ($errno)");        return FALSE;    } $this->log (' client ok ');        } protected static function init () {error_reporting (e_all ^ e_notice);        error_reporting (0);        Set_time_limit (0);        Ob_implicit_flush ();        Date_default_timezone_set (' Asia/shanghai ');        Ignore_user_abort (TRUE);    Mb_internal_encoding (' GBK ');        Public Function SendMessage ($msg) {if ($msg = = =) {return-1;        } try {stream_socket_sendto ($this->client, $msg);        } catch (Exception $exc) {//$this->log ($exc->gettraceasstring ());        }} Public Function GetMessage () {$this->message = Stream_socket_recvfrom ($this->client, 10270000);        $this->log ("received message:"); $this->log ($this->message); Fwrite (STDOUT, $this->message.    "\ r \ n");        } Public Function shutdown () {Stream_socket_shutdown ($this->client, stream_shut_rdwr);    Fclose ($this->client); public static function log ($message) {echo "\ r \ n". $message.    "\ r \ n";    }}//$client = new Socketclient (' 127.0.0.1 ', 12345), while (true) {$msg = Fread (STDIN, 9999999);    if (!trim ($msg)) {continue;    } $client->sendmessage ($msg); $client->getmessage ();}

while (true) {    $msg = socket_accept ($socket);               Accept a socket    if (! $msg) {        echo "socket_accept () failed:". Socket_strerror ($msg). " \ n ";        break;    }    while (true) {        $command = Strtoupper (Trim (Socket_read ($msg, 1024)));//wait for client data        if (! $command) break;        Related processing, such as direct echo        socket_write ($msg, $command, strlen ($command));    }    Socket_close ($msg);    if ($command = = "QUIT")//If you receive an end of communication break        ;} Socket_close ($socket);                   Close socket

Moderator good
Here I need to read the client data analysis to write back to the client side, you want to send the client side quit command to end the socket, this online environment can not be implemented. Is there a better way to do it? Thank you!!
  • Related Article

    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.