hello,
i want to translate the Perl-scriplet to PHP, but i'm apparantly doing something wrong... (i dont know much about Perl either). I hope one of you can help me.
[Perl-scriptlet:]
use IO::Socket;
#just in case if you are going to use web you need autoflush on
$!=1;
my($server) = shift @ARGV;
my($port) = shift @ARGV;
my($MAXLEN) =64; #packet sizes
my($PORTNO) = $port+1; # This port we use when asking players in server..
my $sock = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => $PORTNO,
PeerAddr => $server,
Timeout => 0,
) or die "socket: $@";
#Looping for ever..
while(1) {
$msg="0000"; #not "really" 4 bytes but works enaf to get player number
$sock->send($msg) or die "Send: $!";
print "sending request into $server:$PORTNO\n";
$sock->recv($msg,$MAXLEN) or die "recv: $!";
$hex = a2h($msg);
$hex =~ s/(....).*/$1/;
#next 2 lines is really dirty coding.. cause did't had the Perl docs in handy..
$players_in_hex = $1;
$players_in_hex =~ s/^(..)(..).*/$2$1/;
# .. swapping to get hex in right order..
print "Players:",hex($players_in_hex),"\n";
sleep 5; #like it says.. sleeping 5 secs before going again ..
}
sub a2h {
my $ascii = shift if @_;
my $hex = unpack "H*",$ascii;
return $hex;
};
[End Perl]
I got this so far using PHP, but i think i am doing something wrong while reading the the returned data.
$sock = fsockopen("udp://216.227.226.89", 6001, $errno, $errstr);
if (!$sock) {
echo "$errstr ($errno)<br>\n";
}
else {
fputs($sock, "0000");
$hex = fgets($sock, 8);
unpack("H*", $hex);
echo $hexr;
}
[End PHP]
as you can see, there is alot missing 8)
plz help
~ whizzy