Government Security
Network Security Resources

Jump to content

Photo

Perl Socket Reading Int


  • Please log in to reply
4 replies to this topic

#1 webdevil

webdevil

    Retired GSO General

  • Sergeant Major
  • 1,195 posts

Posted 31 July 2006 - 07:00 PM

I was trying to read in 4 integers, I wanted to know whether this is right?

#!/usr/bin/perl

use IO::Socket::INET;

$MySocket=new IO::Socket::INET->new(PeerPort=>5656,Proto=>'tcp',PeerAddr=>'127.0.0.1');

$MySocket->recv($text,4);

print "\nReceived message '", $text,"'\n";



#2 mulander

mulander

    Private

  • Members
  • 18 posts

Posted 01 August 2006 - 12:18 AM

I was trying to read in 4 integers, I wanted to know whether this is right?

$MySocket->recv($text,4);


I think not, your recv() read in 4 bytes not four integers. Here is a snippet that reads in 4 unsigned integers.

#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;

my $sock = IO::Socket::INET->new(
						PeerPort => some_port,
						PeerHost => some_host,
						) or die "Can't connect: $!";
print "Connected\n";

{
  local $/ = \16; # make <> read in 16 bytes with one swoop.
  my @integers = unpack "IIII", <$sock>;
  print "numbers: @val\n";
}

You can also send back values using the pack method. Btw read perldoc pack and perldoc unpack to learn more about those methods. And also I think you should note that the byte order can be important when reading from sockets.

I hope that helps, have fun.

#3 webdevil

webdevil

    Retired GSO General

  • Sergeant Major
  • 1,195 posts

Posted 01 August 2006 - 02:39 AM

thanks, the program works quite well.


print "numbers: @val\n";
print "numbers: @integers\n";


using pack/unpack isnt very easy, but surely solved my problem.

#4 mulander

mulander

    Private

  • Members
  • 18 posts

Posted 01 August 2006 - 03:28 AM

thanks, the program works quite well.
Although I dont understand the usage unpack. its a little difficult to understand, I would be glad if you could explain it with your own words.


Well the server that you are connecting to, probably sends you the numbers in some structures etc, thats not typical for perl applications, but in C apps its quite common.

So unpack reads the raw data, and transforms it into something perl can understand.

unpack takes several arguments and I stands for unsigned integer.
so unpack "IIII", $raw_data ( 16 bytes in our example ) means:

first argument to unpack is "IIII".
take first char from this arg and check it.
first char is I, I is unsigned int.
unsigned int is 4 bytes, so translate the first for bytes.
check the next character from argument again we have I
extract next 4 bytes and translate,
repeat until done.

So in short, the characters used in unpacks first argument depend on the structure you want to read in from the socket.

#5 webdevil

webdevil

    Retired GSO General

  • Sergeant Major
  • 1,195 posts

Posted 01 August 2006 - 10:10 AM

thanks alot. now I understand why we used unpack. thanks again.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users