Skip to content

Instantly share code, notes, and snippets.

@stefanocudini
Created March 25, 2012 00:56
Show Gist options
  • Save stefanocudini/2190483 to your computer and use it in GitHub Desktop.
Save stefanocudini/2190483 to your computer and use it in GitHub Desktop.
php tcp server
<?
$host = "127.0.0.1";
$port = !isset($argv[1]) ? die("specifica la porta come primo parametro") : $argv[1];
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("C'e' qualche altro programma in ascolta su sta porta\n");
while(1)
{
$spawn = socket_accept($socket);
while(1)
{
$input = socket_read($spawn, 1024);
echo $input;
if(trim($input)=="quit") exit(0);
}
//socket_write($spawn, $out, strlen ($out));
socket_close($spawn);
}
socket_close($socket);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment