Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A question about socket programming

Reply
Thread Tools

A question about socket programming

 
 
firenet
Guest
Posts: n/a
 
      01-12-2007
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

 
Reply With Quote
 
 
 
 
tfe
Guest
Posts: n/a
 
      01-12-2007

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments