AryanSinha has asked for the wisdom of the Perl Monks concerning the following question:

Hello , I am completely new to Perl , networking and socket programming. In the work , I need to send a UDP packet using perl to another IP Address.. Requirements : 1) I need to send a bunch of values (Ex: 23.43,3435.54,1234.34,356.6) at a time. I cannot send them one by one using for loop. 2) The receiving end only accepts numbers . HERE is the script I wrote
#!/usr/bin/perl #udpclient.pl use IO::Socket; use warnings; use Time::HiRes qw (sleep); my ($socket,$data); #my $in_file_name = $ARGV[0]; $in_file_name='C:\Users\Aryan Sinha\Documents\RecordedData.csv'; open(INFILE,"<",$in_file_name) or die "Not able to open test file $!"; $socket = IO::Socket::INET->new (PeerAddr => '21.22.32.214', PeerPort => 5005, Type => SOCK_DGRAM, Proto => 'udp') or die "ERROR in + Socket Creation : $!\n"; while (<INFILE>) { chomp; if($. != 1) #skip first line { $data=$_; # Data would be in String format my @tempData = split(',', $data); #---------------Description of the problem ------------------- +------- # 1) I have a CSV File of numeric values which I need to send +line by line . # 2) $data has a single line of values in String format . Exam +ple : '2,3.4,2323.23,980.3,235.1' # 3) @tempData is an Numeric array where I stored the values s +pliited from $data . Example : (2,3.4,2323.23,980.3,235.1) # # 4) Now importantly , I need to send the data as a "packet of + Numeric values" at a time using SEND SOCKET command in PERL.The Orde +r of Values should not be changed # #-----------------End of the Description --------------------- +--------- $socket->send($DATA_to_be_SENT); sleep(0.25); } } $socket->close();
Please help me out in this. I am completely new to Perl and Just need this small task to finsih to complete rest of my project based on hradwares working on different platform.

Replies are listed 'Best First'.
Re: Send a UDP Packet with bunch of numbers in PERL?
by NetWallah (Canon) on Aug 18, 2016 at 17:43 UTC
    Can you describe what problem you are experiencing when you run your code ?

    The immediate issue I see is that $DATA_to_be_SENT is not initialized.

    Why can't you simply set $DATA_to_be_SENT=$_ ?

    Your problem description is not clear enough. Does "packet of Numeric values" mean binary, fixed size, decimal, or comma separated ? IS the packet fixed size ?

    You may need to do a "chomp" after you read the data, unless you want/need to send the "\n" to the remote end.

    Always "use strict;", particularly if you are a beginner.

            "Software interprets lawyers as damage, and routes around them" - Larry Wall

      This is just a piece of code to explain at a high level. So,

      1) I can't just send the data in string and cannot assign it the $DATA_to_be_SENT = $_ ; as $_ returns a string which in my case would be "-2.3,-4.5,901,223,34.098"

      2) My Receiving end is a hardware which can only work on Floating numbers and all these five numbers should be sent within ONE Packet in the same order.

      3) I noticed that I can do something like this for hexadecimal numbers

      $socket->send("\x-2.3,\x-4.5,\x901,\x223,\x34.098");
      If I try to use "\d-2.3,\d-4.5,\d901,\d223,\d34.098" and observe the network in wireshark , I am seeing the letter d along with the values.

      So I believe you got a better insight of what i am trying to ask.

        Does your hardware accept a number as a string, or does it desire the floating-point encoding of the number? (Yes, this is re-asking what NetWallah already asked, but maybe in a slightly more prominent way, with some extra clarifications following below.)

        • if it wants a string, does it want it the numbers to each be a certain number of characters, or is there a specific delimiter between them (your examples imply a comma)
        • if it wants floating-point encoding of the number, does it want single (32bit), double (64bit), long double (128bit) or something else? what endian-ness? Once you know the exact format of the number it desires, you can split your string on the commas (or use a prebuilt CSV module) to get a list of the floating-point values, then use pack to encode the floating points into the underlying representation that your hardware understands: your format will be one of f (single), d (double), or D (long double); with perl ≥ v5.10, you can add a modifier, > for big-endian or < for little-endian.

        Once you have the answers to those, try coding it up. If you still need help afterwards, show us the coding progress you've made, and what's still not behaving as you expect.

Re: Send a UDP Packet with bunch of numbers in PERL?
by Mr. Muskrat (Canon) on Aug 18, 2016 at 21:31 UTC

    You need to find out exactly what the device on the receiving end is expecting before you can start to implement a solution.

    It sounds like you figured out that the values must be big-endian singles or doubles. However, you seem to be contradicting yourself in your various replies about how they are to be sent. NetWallah and pryrt have tried to get you to understand but I'll take another go at it.

    Does it take a comma separated list of values (like you described with the hex numbers), a single value (so separate packets for each value) or a series of values in some specific format? Perhaps there is documentation that describes said format?

Re: Send a UDP Packet with bunch of numbers in PERL?
by neilwatson (Priest) on Aug 18, 2016 at 17:47 UTC

    Add use strict; to your code to enforce better coding and prevent errors.

    Neil Watson
    watson-wilson.ca