Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

More than one recv() 1

Status
Not open for further replies.

TurdS

Technical User
Apr 6, 2009
7
US
What I'm trying to do is send a string to my UDP server and receive some data back, but more than just once.

Code:
my $sock = IO::Socket::INET->new(Proto     =>'udp',
                              PeerHost  =>$host,
                              PeerPort  =>$port,
			      Reuse => 1) or die $@;
$sock->send($status) or die "send() failed: $:\n";
$sock->recv($data,2000) or die "recv() failed: $:\n";

[BLOCK OF CODE FOR $DATA]

$sock->send($mpr) or die "send() failed: $:\n";
$sock->recv($data2,6000) or die "recv() failed: $:\n";

[BLOCK OF CODE FOR $DATA2]

I'm relatively new to Perl so if this out of order in some way, please let me know...

But what's happening is: Like this, the second recv() fails. If I swap them and call for the other first, the second one still fails...

Long to short: Is there a better (or more correct) way to use the same connection to send and receive multiple times?
 
Are you sure the server isn't closing the connection?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I'm 99% sure the server is not closing the connection.
 
Have you tried just re-establishing the connection and seeing if it works a second time? You can always do a packet capture to see what's going on.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Do you mean doing a close() then doing it all over again? I believe I may have tried that (can't remember), but I will try it. Thanks.
 
Thank you travs!
I did this
Code:
my $sock = IO::Socket::INET->new(Proto     =>'udp',
                              PeerHost  =>$host,
                              PeerPort  =>$port,
			      Reuse => 1) or die $@;
$sock->send($packet);
$sock->recv(my $data,6000) or die "failed to recv() !:\n";
close($sock);
chomp($data);
sleep 1;

I moved my close() right after the recv() and added a sleep. Now it all works perfectly! Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top