User:Woozle/port listener

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< User:Woozle
Revision as of 22:31, 23 June 2012 by Woozle (talk | contribs) (Created page with "==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 firs...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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>