Catch a glimpse of many socket things that do not understand

Source: Internet
Author: User
Looking at socket, a lot of things are unclear. a html5 & nbsp; socket & nbsp; is sent to php. serverphp. some of the server's code functions & nbsp ;__ construct & nbsp; ($ address, & nbsp; $ port) & nbsp; {erro doesn't understand many things about socket.
I sent an html5 socket to php. server.

Php. server does not understand anything.

Some 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, 20) 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 running server. php server. php in linux.
I found
Socket_select ($ changed, $ write = NULL, $ Partition T = NULL, NULL );
It won't run down.
I checked socket_select online.
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 change status. those coming with BSD sockets background will recognize that those socket resource arrays are in fact the so-called file descriptor sets. three independent arrays of socket resources are watched.

According to my understanding, this socket_select can listen to changes in the status of things in the sockets array.
When the status does not change, it is suspended and will not be executed.
Is that true?
------ Solution --------------------
Socket_select () is the end of waiting for all connections to have new dynamics. it has a timeout parameter that specifies how long it can be blocked (which can be understood as running. If the input value is NULL, the function will end after all listening objects have new connections.

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.