http://qs1969.pair.com?node_id=1044159

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

hi..


From the data trace packet, i have found the binary data request as below:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ARPA/9000 NETWORKING^^^^^^^^^^^^^^^^^^^^ +^^^^^^@#% Timestamp : Fri Jul 12 PST 2013 15:30:40.370025 Process ID : [ICS] Subsystem : NS_LS_T +CP User ID ( UID ) : -1 Trace Kind : PDU IN +TRACE Device ID : -1 Path ID : 0 Connection ID : 0 Location : 00123 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +~~~~~~ -------------------------------- TCP Header -------------------------- +-------- sport: 1998 --> dport: 61236 flags: PUSH ACK seq: 0xfd2fed0a urp: 0x0 chksum: 0xe16a data len: 519 ack: 0x2f975faf win: 0x1ed9 optlen: 0 -------------------------------- XOT --------------------------------- +-------- 0: 00 00 02 03 10 01 22 33 20 20 20 20 20 20 20 20 ......"3 + 16: 4a 42 48 42 30 30 32 39 30 30 30 30 30 30 32 30 JBHB00290000002 +0 32: 30 31 30 30 30 30 30 30 30 30 30 30 33 33 31 36 010000000000331 +6 48: 37 36 34 34 31 20 20 20 20 20 20 20 20 20 20 20 76441 + 64: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 + < printing suppressed for one or more repetitions of the previous lin +e > 112: 20 20 20 20 20 20 20 20 20 20 30 32 30 37 32 37 02072 +7 128: 36 33 35 30 37 20 30 32 20 20 20 20 20 20 20 20 63507 02 + 144: 20 20 20 20 20 20 31 33 30 37 31 32 31 34 34 33 130712144 +3 160: 33 32 30 30 30 31 31 30 20 20 20 20 20 20 20 20 32000110 + 176: 4a 42 48 42 30 30 30 34 30 30 30 30 30 30 33 30 JBHB00040000003 +0 192: 30 31 30 30 30 30 30 30 30 30 30 38 39 36 33 32 010000000008963 +2 208: 30 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 00 + 224: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 + < printing suppressed for one or more repetitions of the previous lin +e > 272: 20 20 20 20 20 20 20 20 20 20 30 32 30 37 32 37 02072 +7 288: 36 33 38 30 30 20 30 32 20 20 20 20 20 20 20 20 63800 02 + 304: 20 20 20 20 20 20 31 33 30 37 31 32 31 34 34 34 130712144 +4 320: 31 31 30 30 30 30 33 33 20 20 20 20 20 20 20 20 11000033 + 336: 4a 42 48 42 30 30 31 34 30 30 30 30 30 30 32 30 JBHB00140000002 +0 352: 30 39 30 30 30 30 30 30 30 30 30 35 32 30 37 34 090000000005207 +4 368: 34 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 45 + 384: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 + < printing suppressed for one or more repetitions of the previous lin +e > 432: 20 20 20 20 20 20 20 20 20 20 39 39 00 00 00 00 99... +. 448: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ............... +. < printing suppressed for one or more repetitions of the previous lin +e > 512: 00 00 00 00 00 00 00 -- -- -- -- -- -- -- -- -- ............... +.

My problem is how could i write the perl socket programming to sent the binary data as a request into the remote server ?. Could somebody help me ?

I am very sorry, this is my first experience in socket programming and i very hope somebody can help me..

Thank you,

Replies are listed 'Best First'.
Re: How to sent binary data in socket programming ?
by golux (Chaplain) on Jul 13, 2013 at 18:13 UTC
    Hi bh_perl,

    Check out the module MIME::Base64. Specifically, you can encode your data with encode_base64 and decode it with decode_base64. For example:

    #!/usr/bin/perl -w use strict; use warnings; use MIME::Base64; my $binary = ""; # Create binary data for (my $i = 0; $i < 256; $i++) { $binary .= chr($i); } my $blen = length($binary); print "Binary data is $blen bytes\n\n"; # Encode the data so that it's printable my $enc = MIME::Base64::encode($binary); my $elen = length($enc); print "Encoded data is $elen bytes:\n"; print "$enc\n"; # Decode the data back to binary, compare against original my $dec = MIME::Base64::decode($enc); my $dlen = length($dec); print "Decoded binary is $dlen bytes\n"; if ($dec eq $binary) { print "Decoded data matches original\n"; } else { print "Decoded Data MISMATCH with original\n"; }

    Update:  Fixed broken links; corrected script wording.
    say  substr+lc crypt(qw $i3 SI$),4,5

      hi...

      I think I am able to sent data into remote server. But the recv() function did not display any thing or acknowledgement from the remote server. Could somebody help me ?. My code as below:
      #!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use MIME::Base64; my $host = shift || "19.25.15.22"; my $port = shift || 1998; my $dir = "/data/input"; my $data = `cat $dir/cdr-data.dat |sed 's/ //g'`; my $binary = pack ("H*", $data); print "DATA : $data\n"; print "ASCII: $binary\n"; # auto-flush on socket $| = 1; # create a connecting socket my $socket = new IO::Socket::INET ( PeerHost => $host, PeerPort => $port, Proto => 'tcp', ); die "cannot connect to the server $!\n" unless $socket; print "Connected to server [$host] with port $port\n"; # data to send to a server my $size = $socket->send($binary); print "Sent binary data as request to $host [length $size]\n"; # notify server that request has been sent shutdown($socket, 1); # receive a response of up to 1024 characters from server my $response = ""; $socket->recv($response, 1024); print "received response: $response\n"; $socket->close();
        Hi bh_perl,

        First, how can we know what the result of the following is? ...

        my $dir = "/data/input"; my $data = `cat $dir/cdr-data.dat |sed 's/ //g'`; my $binary = pack ("H*", $data);

        That's a file on your system, which we have no access to.

        You also haven't said anything about the results you get back, which makes it impossible to know how it's failing for you.

        Do you have access to the server-side code? If so, try logging to a local file what's happening on the server side. Make sure you log each step, so if something fails you'll know exactly where in the code it happened.

        If you look at the documentation for recv it suggests perusing "UDP: Message Passing" in perlipc for examples, and referring to that section of the perlipc docs shows an example of recv where the error is displayed:

        $hispaddr = recv(SOCKET, $rtime, 4, 0) || die "recv: $!";

        What happens if you do the same in your code?, ie.:

        $socket->recv($response, 1024); defined($response) || die "recv: $!";

        Update: looking closer at recv shows that it may be the return value from $socket->recv(...) that you want to check for undef, ie:
        $socket->recv($response, 1024) || die "recv: $!";

        say  substr+lc crypt(qw $i3 SI$),4,5
Re: How to sent binary data in socket programming ?
by zork42 (Monk) on Jul 14, 2013 at 14:22 UTC
    From the data trace packet, i have found the binary data request as below:
    How did you obtain the data trace packet?