A lot of things don't understand.

Source: Internet
Author: User
Look at the socket, I don't understand.
I got a HTML5 socket on the internet for Php.server.

There are some places in php.server that don't understand.

Part of the Code

function __construct ($address, $port) {
Error_reporting (E_all);
Set_time_limit (0);
Ob_implicit_flush ();

Socket creation
$this->master = socket_create (Af_inet, Sock_stream, sol_tcp) or Die ("Socket_create () failed");
Socket_set_option ($this->master, Sol_socket, SO_REUSEADDR, 1) or Die ("Socket_option () failed");
Socket_bind ($this->master, $address, $port) or Die ("Socket_bind () failed");
Socket_listen ($this->master) or Die ("Socket_listen () failed");
$this->sockets[] = $this->master;
$this->say ("Server Started:". Date (' y-m-d h:i:s '));
$this->say ("Listening on: {$address} {$port}");
$this->say ("Master socket: {$this->master}\n");

Main Loop
while (true) {

$changed = $this->sockets;
Socket_select ($changed, $write = null, $except = NULL, NULL);

foreach ($changed as $socket) {
if ($socket = = $this->master) {


$client = socket_accept ($this->master);

if ($client < 0) {
$this->log ("Socket_accept () failed");
Continue
}
else {
Connects the socket
$this->connect ($client);
}
}
else {

$bytes = @socket_recv ($socket, $buffer, 2048, 0);
if ($bytes = = 0) {
On Socket.close ();
$this->disconnect ($socket);
}
else {
Retrieve the user from his socket
$user = $this->getuserbysocket ($socket);

if (! $user->handshake) {
$this->dohandshake ($user, $buffer);
}
else {
$this->process ($user, $this->decode ($buffer));
}
}
}
}
}
}


I am now running the server.php PHP server.php under Linux
I found
Socket_select ($changed, $write = null, $except = NULL, NULL);
It's not going to work down.
I checked the socket_select on the Internet.
The address is http://php.net/manual/en/function.socket-select.php.

Socket_select-runs the Select () system call on the given arrays of sockets with a specified timeout

Socket_select () accepts arrays of sockets and waits for them to the change status. Those coming with BSD sockets background would recognize that those socket resource arrays is in fact the so-called file D Escriptor sets. Three independent arrays of socket resources is watched.

According to my understanding, this socket_select can hear changes in the state of things in the sockets array.
When the state doesn't change, it hangs, and it doesn't go down.
You understand that, right?
------Solution--------------------
Socket_select () is the end of waiting for a connection to have a new dynamic, and it is important to have a timeout parameter that specifies how long it can be blocked (and can be interpreted as running). If you pass in NULL, the function will end when there is a new connection for all listeners.
  • 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.