A question about socket programming

F

firenet

My code is like this:

while( 1 )
{
my $i = 0;
my $buffer;
my $from_who = recv( UDP_IN, $buffer, 10, 0 );
if ( $from_who )
{
my ( $the_port, $the_ip ) = sockaddr_in( $from_who );
warn 'Received from ', inet_ntoa( $the_ip ), ": $buffer\n";
$i=$i+1;
print $i."\n";
$data=$data.$buffer;
}
else
{
warn "Problem with recv: $!\n";
}
}

I supposed when i recv a package, $i can increase.But the output is
like this:
Received from 127.0.0.1: bsfsafsfsf
1
Received from 127.0.0.1: sfssfs:w
1
So we can find that the $i increase only one time,where may the problem
be?3ks
 
T

tfe

firenet ha escrito:
My code is like this:

while( 1 )
{
my $i = 0;
my $buffer;
my $from_who = recv( UDP_IN, $buffer, 10, 0 );
if ( $from_who )
{
my ( $the_port, $the_ip ) = sockaddr_in( $from_who );
warn 'Received from ', inet_ntoa( $the_ip ), ": $buffer\n";
$i=$i+1;
print $i."\n";
$data=$data.$buffer;
}
else
{
warn "Problem with recv: $!\n";
}
}

I supposed when i recv a package, $i can increase.But the output is
like this:
Received from 127.0.0.1: bsfsafsfsf
1
Received from 127.0.0.1: sfssfs:w
1
So we can find that the $i increase only one time,where may the problem
be?3ks

the initialisation of $i is inside the loop. On each loop, $i will be
renewed.
Put the line my $i=1 before the while() if you want it to be increased.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top