http://www.perlmonks.org?node_id=1044166


in reply to How to sent binary data in socket programming ?

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

Replies are listed 'Best First'.
Re^2: How to sent binary data in socket programming ?
by bh_perl (Monk) on Jul 14, 2013 at 06:51 UTC

    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

        hi...

        That files contains data on mail first, anyway this is the records of the files:-
        4A 42 48 42 30 30 30 34 30 30 30 30 30 30 33 30 30 31 30 30 30 30 30 3 +0 30 30 30 38 39 36 33 32 30 30 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 20 30 32 30 37 32 37 36 33 38 3 +0 30 20 30 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33 30 37 3 +1 32 31 34 34 34 31 31 30 30 30 30 33 33 20 20 20 20 20 20 20 20 4A 4 +2 48 42 30 30 31 34 30 30 30 30 30 30 32 30 30 39 30 30 30 30 30 30 3 +0 30 30 35 32 30 37 34 34 35 20 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2 +0 20 20 20 20 20 20 20 20 20 20 20 20 39 39 00 00 00 00 00 00 00 00 0 +0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0 +0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0 +0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 <br/> <br>This is the result after i run the script<br/> <code> DATA : 4A4248423030303430303030303033303031303030303030303030383936333 +230302020202020202020202020202020202020202020202020202020202020202020 +202020202020202020202020202020202020202020202020202020202020202020202 +020202020203032303732373633383030203032202020202020202020202020202031 +333037313231343434313130303030333320202020202020204A42484230303134303 +030303030323030393030303030303030303532303734343520202020202020202020 +202020202020202020202020202020202020202020202020202020202020202020202 +020202020202020202020202020202020202020202020202020202039390000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +000000000000000000000000000000000000000000000000000000000000000000000 +00 ASCII: JBHB000400000030010000000008963200 + 02072763800 02 + 130712144411000033 JBHB001400000020090000000005207445 + 99  Connected to server [19.25.15.22] with port 1998 Sent binary data as request to 19.25.15.22 [length 344] Message from Server :

        Based on the result, the recv() did not display anything. Why this is happened ? is it I am wrong ?. For your information, my connection using TCP/IP and connected to cisco router, is it different with UDP ?

        I am trace the data packet using nettl command for hpux server.