From: Hans on
Hi,
I'm trying to write a udp client script, I already have:

my $sock = new IO::Socket::INET (
PeerAddr => $host,
PeerPort => $port,
Proto => $proto,
);
die "Could not create socket: $!\n" unless $sock;
my $do =1;
while $do {
print $sock "ping request from initiator,id is $i \n";
if ($line = <$sock>) {
print "receive: $line \n";
}
$i++;
sleep(1);
}


My problem is it will always wait to receive something before sending
next ping request. if no response from server, then it stops sending.

I hope it can keep sending no matter if there is response.

Anybody can help? thanks!


Hans
From: Ben Morrow on

Quoth Hans <hansyin(a)gmail.com>:
> Hi,
> I'm trying to write a udp client script, I already have:
>
> my $sock = new IO::Socket::INET (
> PeerAddr => $host,
> PeerPort => $port,
> Proto => $proto,
> );
> die "Could not create socket: $!\n" unless $sock;
> my $do =1;
> while $do {

Please only post code *which you have actually run*.

> print $sock "ping request from initiator,id is $i \n";
> if ($line = <$sock>) {
> print "receive: $line \n";
> }
> $i++;
> sleep(1);
> }
>
>
> My problem is it will always wait to receive something before sending
> next ping request. if no response from server, then it stops sending.
>
> I hope it can keep sending no matter if there is response.

You need to put the socket in non-blocking mode and write a select loop,
or use some module like POE that wraps that for you. (An alternative
would be to go multi-process/thread, but unless this is a tiny program
that quickly becomes more trouble than the alternative.)

Ben