3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php$host = 'localhost'; $port = 7776; $null = NULL; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); socket_bind($socket, 0, $port); socket_listen($socket); $clients = array($socket); while (true) { $changed = $clients; @socket_select($changed, $null, $null, 0, 10); if (in_array($socket, $changed)) { $socket_new = @socket_accept($socket); $clients[] = $socket_new; $header = socket_read($socket_new, 1024); //perform_handshaking($header, $socket_new, $host, $port); socket_getpeername($socket_new, $ip); $found_socket = array_search($socket, $changed); unset($changed[$found_socket]); } foreach ($changed as $changed_socket) { while(@socket_recv($changed_socket, $buf, 1024, 0) >= 1) { //handle messages } $buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ); //disconnect state if ($buf === false) { $found_socket = array_search($changed_socket, $clients); @socket_getpeername($changed_socket, $ip); unset($clients[$found_socket]); } } } socket_close($sock);
Output for 5.3.0 - 5.4.32
<?php$host = 'localhost'; $port = 7776; $null = NULL; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1); socket_bind($socket, 0, $port); socket_listen($socket); $clients = array($socket); while (true) { $changed = $clients; @socket_select($changed, $null, $null, 0, 10); if (in_array($socket, $changed)) { $socket_new = @socket_accept($socket); $clients[] = $socket_new; $header = socket_read($socket_new, 1024); //perform_handshaking($header, $socket_new, $host, $port); socket_getpeername($socket_new, $ip); $found_socket = array_search($socket, $changed); unset($changed[$found_socket]); } foreach ($changed as $changed_socket) { while(@socket_recv($changed_socket, $buf, 1024, 0) >= 1) { //handle messages } $buf = @socket_read($changed_socket, 1024, PHP_NORMAL_READ); //disconnect state if ($buf === false) { $found_socket = array_search($changed_socket, $clients); @socket_getpeername($changed_socket, $ip); unset($clients[$found_socket]); } } } socket_close($sock);