PHP Stream Learning Note one (synchronous blocking IO model)

Source: Internet
Author: User
Tags fread

Original http://blog.csdn.net/shagoo/article/details/6396089[[email protected] PHP]#VI server_one.php<?PHPSet_time_limit(0);classSocketserver {Private Static $socket;  Public function__construct ($port)     {        Global $errno,$errstr; if($port< 1024) {             die("Port must be a number which bigger than 1024/n"); }                $socket=Stream_socket_server("tcp://0.0.0.0:{$port}",$errno,$errstr); if(!$socket) die("$errstr($errno)"); //Stream_set_timeout ($socket,-1);//Ensure that the server socket does not time out, it seems useless:)         while($conn=stream_socket_accept($socket,-1)) {//This setting does not time out before the oil is agreed with a connection and returns the other socket socket            Static $id= 0; Static $ct= 0; $ct _last=$ct; $ct _data= ' '; $buffer= ' '; $id++;//increase on each accept            EchoThe Client$idcome./n ";  while(!Preg_match('/(\/R)? \/n/',$buffer)) {//do not read to Terminator, continue to read//if (feof ($conn)) break;//Prevent Popen and fread bugs from causing a dead loop                $buffer=fread($conn, 1024); Echo' R '.Php_eol;//number of print reads                $ct+=strlen($buffer); $ct _data.=Preg_replace('/\/r?\/n/', ',$buffer); }            $ct _size= ($ct-$ct _last) * 8; Echo"[$id] " .__method__. ">".$ct _data. "/n".Php_eol; fwrite($conn, "Received I am the server I just received the data you sent to the size of$ct _sizeByte data "." /r/n "); Echo' The one who's connected to me, I wrote you a reply. '; fclose($conn); }                fclose($socket); }}$a=NewSocketserver (2000); [[email protected] PHP] #php server_one.php[[email protected] php]#VI client_one.php<?PHPfunctionDebug ($msg)  {      Echo $msg; Error_log($msg." \ r \ n ", 3, '/tmp/tmp_socket.log '); }if($argv[1]){    $socket _client=stream_socket_client(' tcp://0.0.0.0:2000 ',$errno,$errstr, 30); stream_set_blocking($socket _client, 0);//If the mode is 0, the given stream will turn to non-blocking mode 1 blocking mode    Stream_set_timeout($socket _client, 0, 100000);//The second argument is whether the time-out of the second parameter sets the third parameter the number of seconds default microseconds    if(!$socket _client) {         die("$errstr($errno)"); } Else {        $msg=Trim($argv[1]);  for($i= 0;$i< 10;$i++) {            $res=fwrite($socket _client, "$msg($i)"); Usleep(100000);//A delay of 1 microseconds (micro second) with a specified number of microseconds is one out of 10,000 seconds.             Echo' W ';//number of print writes//debug (fread ($socket _client, 1024));//will generate a deadlock because fread will wait for the data to be read in blocking mode        }        fwrite($socket _client, "/r/n");//transfer Terminator       Sleep(2);//Note that you don't sleep for 2 seconds, don't write logs .Debugfread($socket _client, 10024)); fclose($socket _client); }} Open three clients repeat the following command when the server response results only provide one person's response service [[email protected] PHP]#telnet 127.0.0.1CRTL +]telnet>Direct return123234abc

PHP Stream Learning Note one (synchronous blocking IO model)

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.