A question about socket programming

Discussion in 'Perl Misc' started by firenet, Jan 12, 2007.

  1. firenet

    firenet Guest

    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
    firenet, Jan 12, 2007
    #1
    1. Advertising

  2. firenet

    tfe Guest

    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.

    --

    tfe
    http://tfeserver.be
    tfe, Jan 12, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,816
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    965
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    545
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    661
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    715
Loading...

Share This Page