User:Woozle/port listener
About
This is as much a demonstration script as it is a utility. It listens at a hard-coded address and port, and displays any incoming connections. It also takes the first connection and accepts it, then displays any incoming data (not necessarily in a tidy way).
Code
<php><?php $rcS = socket_create(AF_INET,SOCK_STREAM,SOL_TCP ); $ok = socket_bind($rcS,'localhost',9000); if ($ok) { echo "Socket bound.\n"; $ok = socket_listen($rcS); if ($ok) { echo "Listening for connections on socket.\n";
while (TRUE) { if(($rcCNew = socket_accept($rcS)) !== false) { echo "Client $rcCNew has connected\n"; $rcC = $rcCNew; }
$str=socket_read($rcC,255,PHP_NORMAL_READ); if ($str != ) { echo "RECEIVED: [$str]\n"; } } } else { echo "Could not listen to socket."; $err = socket_last_error($rc); $strOut = 'Error code '.$err.': '.socket_strerror($err); echo ' '.$strOut; } } else { echo "Could not bind socket.\n"; }</php>