Stream_select How to determine socket connection and disconnection?

Source: Internet
Author: User
Keywords Php
Tags fread php website
This is an example of Stream_select on the PHP website.
http://php.net/manual/en/function.stream-socket-server.php


  

程序是从遍历连接的客户端是不是serversocket来判断连接, 遍历每个客户端都fread来读,根据读的结果是FALSE还是0判断是不是断开连接了,但是上面的程序一运行,cpu就上升,然后机器就卡住了,是不是这种判断连接有断开的方式有问题?

Reply content:

This is an example of Stream_select on the PHP website.
http://php.net/manual/en/function.stream-socket-server.php


   

The

program is from traversing the connected client is not serversocket to determine the connection, traversing each client is fread to read, according to whether the read result is False or 0 to determine whether it is disconnected, but the above program runs, the CPU goes up, then the machine is stuck , is there a problem with the way the connection is broken?

Run this example and report a bunch of errors, you didn't error?
I modified the

  $master = Array (); $socket = Stream_socket_server ("tcp://0.0.0.0:8000", $    errno, $ERRSTR); $master [] = $socket; $read = $master; while (1) {$read = $master;    $_w = $_e = null;    $mod _fd = Stream_select ($read, $_w, $_e, 0);        foreach ($read as $rstream) {$conn = Stream_socket_accept ($socket); if ($conn) {fwrite ($conn, "hello! The time is. Date ("n/j/y g:i a").            "\ n");            $sock _data = fread ($conn, 1024x768);//Var_dump ($sock _data); if ($sock _data = = = 0) {} elseif ($sock _data = = = False) {echo "Something bad happened".            Php_eol; } else {fwrite ($conn, "You have sent: [". $sock _data.            "]\n");        } fclose ($conn); }}}  
  • 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.