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!

UDP recv() not receiving all the data

Status
Not open for further replies.

TurdS

Technical User
Apr 6, 2009
7
US
I'm using UDP (because the server only accepts UDP, I didn't write it) to receive some data from the server and it's not getting all the data. I know there's at least 18-19 lines I need to receive but I'm only getting 13-14 of them. I've tried increasing the length and even adding a sleep to see if it just needs more time to get it all but nothing. I still only get so many lines back. Anyone got any tips?
Code:
my $sock = IO::Socket::INET->new(Proto =>'udp',
				 PeerHost =>$host,
				 PeerPort =>$port,
				 Reuse => 1) or die $@;
my $packet = "getinfo";
$sock->send($packet);
$sock->recv($data,6000) or die "failed to recv() !:\n";
sleep 5;
print $data;

Like I mentioned, I've tried 6000 to 30000, still only so much comes through. No errors or nothing.

Side question: is there anyway to dump the returned data to an array instead of a scalar? I've tried using @data instead of $data and it gives me a usage error:
Code:
usage: $sock->recv(BUF, LEN [, FLAGS]) at status.pl line 18
 
afaik it has to return the output to a scalar. If you want it broken up into lines, do it separately, with this:

Code:
my @lines = split(/\n/, $data);

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Yeah that's what I figured, I was just curious if it was possible.
 
Not sure if it helps but here's my production code, it was written about 8 years ago but has never broken.
Code:
### Socket
my $sock = new IO::Socket::INET (LocalHost => "MyHostName",
                               LocalPort => 5000,
                               Listen    => 5,
                               Proto     => 'tcp',
                               Reuse     => 1,
                              );
die "Socket could not be created. Reason: $!\n" unless ($sock);

#Wait for new connections
while (my $new_sock = $sock->accept())
{
        $new_sock->autoflush(1);
        #Fork off connection so I can listen for more connections
        my $pid = fork();
        if ($pid == 0)
        {
                close($sock);
                my @sockarray;

                #Read in buffer
                while (my $buf = <$new_sock>)
                {
                        #push info into an array
                        push @sockarray, $buf;

                        #if we get bye we're done
                        if ($buf =~ /bye/)
                        {
                                close($new_sock);
                        }
                }

                #Get rid of new lines
                chomp @sockarray;

                #Do stuff with @sockarray;
                exit;
        }
        close($new_sock);
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
Wow, thanks again Travis but, sadly I have to use the server that I'm using and I have no access to change it. I also need to use UDP. But I will save this for something I need to do later. Thanks!
 
You should be able to just change the TCP to UDP there's nothing else in my code that is specific to a server.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[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;
 
UDP doesn't marshal the packets like TCP (or even guarantee that they will arrive at all, or in what order). You may find that you have to issue multiple recv calls to get the whole message - the server may be sending it as multiple packets, and just beefing up your buffer won't change this.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top