arrays - I cannot modify hex string for socket communication -


i'm newbie perl programming. i'm trying send hex string on socket. below perl code i'm writing. works perfectly. need modify hex string 7th byte 10th byte ip address.

#!/usr/bin/perl -w use io::socket::inet; $data= "\x0f\x00\x00\x00\x00\x00\x0a\x14\x1f\x01"; # create socket $sock = new io::socket::inet(       peeraddr=>'192.168.1.101',       peerport=>'8121',       proto=>'udp) or die $!; # send data server $sock->send($data); $sock->close(); exit(0); 

but don't know how modify ip address section of $data. if use below,

my @data= (0x0f,0x00,0x00,0x00,0x00,0x00,0x0a,0x14,0x1f,0x01); 

i can access data , modify below.

$data[6] = 0x0a; $data[7] = 0x14; $data[8] = 0x1f; $data[9] = 0x02;         

but don't know how modify $data this. please give me idea! in advance.

my $data = join '', map chr, @data; 

or

my $data = pack 'c*', @data; 

of course, don't need convert whole string individual numbers.

my @ip_octets = ( 10, 20, 31, 1 ); $data= "\x0f\x00\x00\x00\x00\x00" . pack('c*', @ip_octets); 

better yet, let's start actual ip address.

use socket qw( inet_aton );  $ip_addr = '10.20.31.1'; $data= "\x0f\x00\x00\x00\x00\x00" . inet_aton($ip_addr); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -