Created
May 18, 2011 07:46
-
-
Save luciferous/978156 to your computer and use it in GitHub Desktop.
Various blocking and non-blocking IO demos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ss = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
$iface = '0.0.0.0'; | |
$port = 5432; | |
socket_set_option($ss, SOL_SOCKET, SO_REUSEADDR, 1); | |
socket_bind($ss, $iface, $port) or die("Could not bind\n"); | |
socket_listen($ss, 1); | |
print "Listening on $iface:$port\n"; | |
while (true) { | |
$cs = socket_accept($ss); | |
socket_getpeername($cs, $addr, $port); | |
print "Client connected $addr:$port\n"; | |
while (true) { | |
$ok = socket_recv($cs, $data, 1024, 0); | |
if (!$ok) { | |
break; | |
} | |
socket_send($cs, $data, 1024, 0); | |
print "$data"; | |
} | |
print "Closing client socket\n"; | |
socket_close($cs); | |
} | |
print "Closing master socket\n"; | |
socket_close($ss); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ss = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
$iface = '0.0.0.0'; | |
$port = 5432; | |
socket_set_option($ss, SOL_SOCKET, SO_REUSEADDR, 1); | |
socket_bind($ss, $iface, $port) or die("Could not bind\n"); | |
socket_listen($ss, 1); | |
print "Listening on $iface:$port\n"; | |
$clients = array(); | |
$sendbuf = array(); | |
$wantswrite = array(); | |
$nicks = array(); | |
while (true) { | |
$ws = $wantswrite; | |
$rs = $clients; | |
$rs[] = $ss; | |
$len = socket_select($rs, $ws, $xs, 10); | |
if ($len === 0) { | |
foreach ($clients as $client) { | |
$wantswrite[$client] = $client; | |
$sendbuf[$client] = "<server heart beat>\n"; | |
print "Buffering to send to client #$client: $data"; | |
} | |
} | |
foreach ($rs as $cs) { | |
if ($cs == $ss) { | |
$cs = socket_accept($ss); | |
socket_getpeername($cs, $addr, $port); | |
print "Connected: client #$cs $addr:$port\n"; | |
$clients[$cs] = $cs; | |
$wantswrite[$cs] = $cs; | |
$sendbuf[$cs] = "You are connected to the chat room." | |
. " Type 'quit' to quit.\n\nPlease enter your name: "; | |
print "Buffering to send to client #$cs: $data"; | |
} else { | |
$ok = socket_recv($cs, $data, 1024, 0); | |
if ($data == '') { | |
print "Closing: client #$cs\n"; | |
socket_close($cs); | |
unset($clients[$cs]); | |
unset($nicks[$cs]); | |
} else { | |
if (empty($nicks[$cs])) { | |
$nicks[$cs] = trim($data); | |
$wantswrite[$cs] = $cs; | |
$sendbuf[$cs] = "You are now known as $data"; | |
print "Buffering to send to client #$cs: $data"; | |
} else { | |
$message = $nicks[$cs] . ': ' . $data; | |
if (trim($data) == 'quit') { | |
$message = $nicks[$cs] . " has left the channel.\n"; | |
print "Closing: client #$cs\n"; | |
socket_close($cs); | |
unset($clients[$cs]); | |
unset($nicks[$cs]); | |
} | |
foreach ($clients as $client) { | |
$wantswrite[$client] = $client; | |
$sendbuf[$client] = $message; | |
print "Buffering to send to client #$client: $data"; | |
} | |
} | |
} | |
} | |
} | |
foreach ($ws as $cs) { | |
$data = $sendbuf[$cs]; | |
print "Sending data to client #$cs: $data"; | |
socket_send($cs, $data, 1024, 0); | |
unset($sendbuf[$cs]); | |
unset($wantswrite[$cs]); | |
} | |
} | |
print "Closing master socket\n"; | |
socket_close($ss); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ss = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
$iface = '0.0.0.0'; | |
$port = 5432; | |
socket_set_option($ss, SOL_SOCKET, SO_REUSEADDR, 1); | |
socket_bind($ss, $iface, $port) or die("Could not bind\n"); | |
socket_listen($ss, 1); | |
print "Listening on $iface:$port\n"; | |
$clients = array(); | |
$sendbuf = array(); | |
$wantswrite = array(); | |
while (true) { | |
$ws = $wantswrite; | |
$rs = $clients; | |
$rs[] = $ss; | |
socket_select($rs, $ws, $xs, 10); | |
foreach ($rs as $cs) { | |
if ($cs == $ss) { | |
$cs = socket_accept($ss); | |
socket_getpeername($cs, $addr, $port); | |
print "Connected: client #$cs $addr:$port\n"; | |
$clients[$cs] = $cs; | |
} else { | |
$ok = socket_recv($cs, $data, 1024, 0); | |
if ($data == '') { | |
print "Closing: client #$cs\n"; | |
socket_close($cs); | |
unset($clients[$cs]); | |
} else { | |
$wantswrite[$cs] = $cs; | |
$sendbuf[$cs] = $data; | |
print "Buffering to send to client #$cs: $data"; | |
} | |
} | |
} | |
foreach ($ws as $cs) { | |
$data = $sendbuf[$cs]; | |
print "Sending data to client #$cs: $data"; | |
socket_send($cs, $data, 1024, 0); | |
unset($sendbuf[$cs]); | |
unset($wantswrite[$cs]); | |
} | |
} | |
print "Closing master socket\n"; | |
socket_close($ss); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment