1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| #!/usr/bin/perl -w
use strict;
use IO::Socket;
my($sc, $newmsg, $port, $ipaddr, $arret, $boucle, $x, $y, $dir, $MAXLEN, $PORTNO, $nbBoucle);
$arret = 0;
$boucle = 0;
$x = 0;
$y = 0;
$dir = 0;
$MAXLEN = 20;
$PORTNO = 1027;
$sc = IO::Socket::INET->new( LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@";
print "[serveur.pl] Serveur demarre sur port $PORTNO\n";
while ( $arret == 0 )
{
if ( $boucle == 0 )
{
if ( $sc->recv( $newmsg, $MAXLEN, MSG_DONTWAIT ) )
{
($port,$ipaddr) = sockaddr_in($sc->peername);
print "[serveur.pl] ($port) ($ipaddr) Recu $newmsg\n";
if ( $newmsg =~ "^GO+" )
{
print "[serveur.pl] Demarrage boucle\n";
$boucle = 1;
}
if ( $newmsg =~ "^STOP+" )
{
print "[serveur.pl] Arret boucle\n";
$boucle = 0;
$nbBoucle = 5;
}
}
}
if ( $boucle == 1 )
{
if ( $dir == 0 )
{
if ( $x == 100 )
{
$y++;
if ( $y == 100 )
{
$dir = 1;
}
}
else
{
$x++;
}
}
else
{
if ( $x == 0 )
{
$y--;
if ( $y == 0 )
{
$dir = 0;
}
}
else
{
$x--;
}
}
$sc->send( $ipaddr.";".$x.";".$y."\0\n", 0, $sc->peername );
sleep ( 1 );
}
}
die "recv: $!"; |
Partager