in reply to XML not reading fully during IO::Socket receive

Is this the very first read from the socket? If not, I would guess that previous reads from that socket include the first couple of bytes of the XML content.

It looks like you code is in a loop (and I'm also inferring that since you say the problem happens "after a while".) What does the rest of your code look like?

  • Comment on Re: XML not reading fully during IO::Socket receive

Replies are listed 'Best First'.
Re^2: XML not reading fully during IO::Socket receive
by deadpickle (Pilgrim) on Jul 08, 2008 at 18:15 UTC
    Here is the entire subroutine that handles the telemetry. It is a rapid update, I think it sends an XML every second or so, basically one after another.
    sub telemetry { $SIG{PIPE} = "IGNORE"; $\="\n"; my $img = '"uas.png"'; my $post_time; my $test; my $xs = XML::Simple->new( RootName=>'Telemetry'); my $check = 0; while(1){ goto END if $die == 1; if ($tele_start == 1){ my $tele_client = IO::Socket::INET->new( PeerAddr => $node + ) or $error = 3; if ($tele_client){ for(;;){ $telemetry = 1; print "@@@ started @@@"; print "*** Recv UA ***"; $tele_client->recv($test, 1599); print $test; #use bytes; print "bytes: ",length($test); print "Reading in XML"; #if (length $test <= 700){ my $uasdata = $xs->XMLin($test); print "XML Loaded!"; if ("$uasdata->{VehicleID}" eq 'Velocity' and defi +ned $uasdata){ #Open Placefile open UASPF,'>',"uaspf.txt.bak" or die "Cannot +Open File uavpf.txt.bak!"; #open UASPF,'>',"/home/uas/public_html/uas/uas +pf.txt.bak" or die "Cannot Open File uavpf.txt.bak!"; #&ctime; my @time = gmtime; my $hour = $time[2]; my $min = $time[1]; my $sec = $time[0]; $hour = "0$time[2]" if $time[2] < 10; $min = "0$time[1]" if $time[1] < 10; $sec = "0$time[0]" if $time[0] < 10; $post_time = "($hour:$min:$sec)"; #print placefile elements print UASPF "Title: UAS GPS Position"; print UASPF "Threshold: 999"; print UASPF "RefreshSeconds: 2"; print UASPF "Iconfile: 1, 22, 21, 10, 14, $img +\n"; print UASPF "Icon: $uasdata->{GPS}->{Latitude} +, $uasdata->{GPS}->{Longitude}, $uasdata->{GPS}->{Heading}, 1, 1, $po +st_time Latitude: $uasdata->{GPS}->{Latitude} Longitude: $uasdata->{G +PS}->{Longitude} Heading: $uasdata->{GPS}->{Heading} Speed: $uasdata- +>{AirData}->{TrueAirSpeed} cm/s"; close UASPF; #rename("/home/uas/public_html/uas/uaspf.txt.b +ak", "/home/uas/public_html/uas/uaspf.txt") or print "Cannot rename f +ile!";# and $log->error($!); rename("uaspf.txt.bak", "uaspf.txt") or die"Un +able to replace: $!"; print "*** UA Done ***"; } #} goto END if $die == 1; last if $tele_start == 0; } undef $tele_client; }else { $telemetry = 0}; print "SLEEP"; sleep 5; } else {sleep 1}; } END: }
      I believe I have solved my problem. I switched from using XML::Simple to using XML::Smart. In the module there is a parser called XML::Smart::HTMLParser which handles badly formated XML's. It caught the error and handled it. Great.