Hallo Leute,
ich habe folgenden PHP-Code:
[code=php]
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$packet = chr(255).chr(255)."Z".chr(255);
$bytes_sent = socket_sendto($socket, $packet, strlen($packet), 0, $host, $port);
echo "<b>".$bytes_sent."</b> bytes <b>OK</b></li>";
$recv_bytes = 0;
$buffer = "";
$timeout = 30;
$answer = "";
$packets = 0;
$read = array($socket);
while (socket_select($read, $write = NULL, $except = NULL, &$timeout) > 0) {
$recv_bytes += socket_recvfrom($socket, &$buffer, 2000, 0, &$host, &$port);
if (($buffer[0] == chr(255)) && ($buffer[1] == chr(255)) && ($buffer[2] == "Z") && ($buffer[3] == chr(255)) &&
($buffer[4] == "1") && ($buffer[5] == ".") && ($buffer[6] == "0") && ($buffer[7] == "0") && ($buffer[8] == chr(255))) {
$answer .= substr($buffer, 9, strlen($buffer));
}
$buffer = "";
$timeout = "1";
$packets++;
}
[/code]
Den würde ich gerne in Perl umsetzen.
Und meiner sieht bis jetzt so aus:
[ocde=perl]
use strict;
use IO::Socket;
my $HOSTNAME = '127.0.0.1';
my $PORTNO = '27501';
my $length = 1024;
socket (SOCKET, PF_INET , SOCK_DGRAM, getprotobyname ('udp'))
or die "socket: $!";
my $ipaddr=inet_aton($HOSTNAME);
my $portaddr = sockaddr_in($PORTNO, $ipaddr) ;
my $msg = chr(0xff).chr(0xff)."Z".chr(0xff);
send (SOCKET, $msg, 0 , $portaddr) == length ($msg)
or die "cannot send to $HOSTNAME($PORTNO): $!";
my $rin = '';
vec($rin, fileno(SOCKET), 1) = 1;
my $rout;
while (select($rout = $rin, undef, undef, 10.0))
{
$msg = '';
$portaddr = recv(SOCKET, $msg , $length, 0)
or die "recv: $!";
($PORTNO, $ipaddr) = sockaddr_in ($portaddr);
my $host = gethostbyaddr ($ipaddr, AF_INET);
print "$host($PORTNO) said $msg$/";
}
[/code]
Ist die Umsetzung bisher richtig oder würdet ihr das anders machen?
Wäre über Ideen, Verbesserungsvorschläge, etc. sehr dankbar!
Danke im Voraus.