php SOCKET-server
В исходном состоянии:
<?php
phpinfo();
die();
//header("Content-type: text/xml");
function check( $value, $function )
{
if( $value<0 ) die ( "<error>function [ $function ] returns ". strerror($value)."</error>");
else echo ( "<okay >function [ $function ] returns $value. (Success) </okay>");
superflush();
}
function superflush()
{
for($i=3;$i;$i--){
for($j=5;$j;$j--) print(" "); flush(); }
echo "\n";
}
function closedown()
{
global $port, $listener, $guests;
echo "<release> Freeing Port $port and this list:
";
echo "Listening Socket ".$listener."
";
for( $i=0; $i<count($guests); $i++) {
echo "Client Socket ".$guests[$i]."
";
close( $guests[$i] );
}
echo "</release>";
superflush();
}
set_time_limit(120);
register_shutdown_function ("closedown" );
$address = gethostbyname ($SERVER_NAME);
$port = 2536;
$maxport = 2570;
$guests = array();
$hi= "<WELCOME>
Hello World!!!
</WELCOME>\0";
check( $listener = socket(AF_INET,SOCK_STREAM,0), "socket" );
while( ($errno = bind( $listener, $address, $port)) <0 )
if( ++$port > $maxport)
die("<error>Tried ports up to $port, result is ".strerror( $errno )."</error>" );
else
{ echo("<try>Got [".strerror($errno)."] Now trying port $port
</try>\n" ); superflush();}
print( "<port>$port</port>" );
superflush();
check( $ret = listen( $listener, 5) , "listen" );
set_nonblock ( $listener );
do {
if( ($tmp= accept_connect( $listener )) >0 ){
write( $tmp, $hi, strlen($hi));
array_push( $guests, $tmp );
}
$bufs=array();
$buf="";
for ($i=0; $i< count( $guests ); $i++)
if( read( $guests[$i], $buf, 48) )
array_push( $bufs, "<Omsg>".strtoupper( strip_tags( $buf) )."</Omsg>\0" );
print( " counts: ".$counter." bufs:".count($bufs)." guests: ".count($guests)."
\n" );
for ($b=0; $b< count($bufs); $b++)
print( $bufs[$b]."
" );
superflush();
for ($i=0; $i< count( $guests ); $i++)
for ($b=0; $b< count($bufs); $b++)
$writeerr = write($guests[$i], $bufs[$b], strlen($bufs[$b]));
sleep(1);
} while ( $counter++<120 && !stristr( $buf,"quit") );
closedown();
?>
В немного подпрваленном:
<?
die();
header("Content-type: text/xml");
function check( $value, $function )
{
if( $value<0 ) die ( "<error>function [ $function ] returns ". strerror($value)."</error>");
else echo ( "<okay >function [ $function ] returns $value. (Success) </okay>");
superflush();
}
function superflush()
{
for($i=3;$i;$i--){
for($j=5;$j;$j--) print(" "); flush(); }
echo "\n";
}
function closedown()
{
global $port, $listener, $guests;
echo "<release> Freeing Port $port and this list:
";
echo "Listening Socket ".$listener."
";
for( $i=0; $i<count($guests); $i++) {
echo "Client Socket ".$guests[$i]."
";
close( $guests[$i] );
}
echo "</release>";
superflush();
}
set_time_limit(120);
register_shutdown_function ("closedown" );
$address = gethostbyname ($SERVER_NAME);
$port = 2536;
$maxport = 2570;
$guests = array();
$hi= "<WELCOME>
HELLO WORLD!!!
</WELCOME>\0";
check( $listener = socket_create(AF_INET,SOCK_STREAM,0), "socket" );
while( ($errno = socket_bind( $listener, $address, $port)) <0 )
if( ++$port > $maxport)
die("<error>Tried ports up to $port, result is ".strerror( $errno )."</error>" );
else
{ echo("<try>Got [".strerror($errno)."] Now trying port $port
</try>\n" ); superflush();}
print( "<port>$port</port>" );
superflush();
check( $ret = listen( $listener, 5) , "listen" );
set_nonblock ( $listener );
do {
if( ($tmp= accept_connect( $listener )) >0 ){
write( $tmp, $hi, strlen($hi));
array_push( $guests, $tmp );
}
$bufs=array();
$buf="";
for ($i=0; $i< count( $guests ); $i++)
if( read( $guests[$i], $buf, 48) )
array_push( $bufs, "<Omsg>".strtoupper( strip_tags( $buf) )."</Omsg>\0" );
print( " counts: ".$counter." bufs:".count($bufs)." guests: ".count($guests)."
\n" );
for ($b=0; $b< count($bufs); $b++)
print( $bufs[$b]."
" );
superflush();
for ($i=0; $i< count( $guests ); $i++)
for ($b=0; $b< count($bufs); $b++)
$writeerr = write($guests[$i], $bufs[$b], strlen($bufs[$b]));
sleep(1);
} while ( $counter++<120 && !stristr( $buf,"quit") );
closedown();
?>