Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll answer it myselfe. I went over it line by line.. this is how it works (I'm only explaining the part in the while(true) loops.</p> <p>1.</p> <pre><code>// Setup clients listen socket for reading $read[0] = $sock; for ($i = 0; $i &lt; $max_clients; $i++) { if (isset($client[$i]['sock'])) $read[$i + 1] = $client[$i]['sock']; } </code></pre> <p>This asings freshly created connections to the $read array to be watched for incoming data.</p> <pre><code>// Set up a blocking call to socket_select() if (socket_select($read, $write = NULL, $except = NULL, $tv_sec = 5) &lt; 1) continue; </code></pre> <p>Watches the $read array for new data (I'm still a bit unclear how this works)</p> <pre><code>/* if a new connection is being made add it to the client array */ if (in_array($sock, $read)) { for ($i = 0; $i &lt; $max_clients; $i++) { if (empty($client[$i]['sock'])) { $client[$i]['sock'] = socket_accept($sock); echo "New client connected $i\r\n"; break; } elseif ($i == $max_clients - 1) echo "Too many clients...\r\n"; } } </code></pre> <p>Determines when a new connection is being made, than finds an empty spot in the $client array and add the socket.</p> <p>This next part I'll split up for easier explanation.</p> <pre><code>for ($i = 0; $i &lt; $max_clients; $i++) { // for each client if (isset($client[$i]['sock'])) { </code></pre> <p>Loops through all the $client array but only works on the ones that actually have a connection.</p> <pre><code>if (in_array($client[$i]['sock'], $read)) { $input = socket_read($client[$i]['sock'], 1024); if ($input == null) { echo "Client disconnecting $i\r\n"; // Zero length string meaning disconnected unset($client[$i]); } else { echo "New input received $i\r\n"; // send it to the other clients for ($j = 0; $j &lt; $max_clients; $j++) { if (isset($client[$j]['sock']) &amp;&amp; $j != $i) { echo "Writing '$input' to client $j\r\n"; socket_write($client[$j]['sock'], $input, strlen($input)); } } if ($input == 'exit') { // requested disconnect socket_close($client[$i]['sock']); } } } else { echo "Client disconnected $i\r\n"; // Close the socket socket_close($client[$i]['sock']); unset($client[$i]); } </code></pre> <p>First it sees if there is still an active connection, if not it closes it. If there is a connection it read the data, if there is noone this is code for a disconnect. If there is data it passes it along to other clients (but itselfe).</p> <p>That's ti. Hope I got it right.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload