|
Posted by unxl3arn3r on March 25, 2008, 1:51 pm
Please log in for more thread options
> Quoth xhos...@gmail.com:
>
>
>
> > > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > > > Gurus
> > > > > I seem to be unable to pack a string padded with nulls and
> > > > > containing the length of the string prepended to it. This is my code
> > > > > snippet, where am i going wrong ?
>
> > > > > $message1 = pack("l! a*",1, "This is a test,This program is free
> > > > > software,you can redistribute it terms as Perl network, This is why I
> > > > > said to talk");
>
> > > > l! packs the "1", and a* packs the message. No where in there does the
> > > > length of the message enter into it.
>
> > > > I see that that is copied from the perldoc -f msgsnd. I can only
> > > > assume that that documentation is hosed.
>
> > > > Perhaps it should be this, instead?
>
> > > > pack("l! l!/a*",1,"foo")
>
> > > > Here l! packs the message type (1), l!/ packs the length of the
> > > > message, and a* packs the message itself.
>
> > > > > msgsnd($queue,$message1,0);
>
> > > why do I need to have the second variable in pack ?
>
> > I don't understand your question. None of the packs I've seen in this
> > thread are given two variables. They are given three constants--a constant
> > format string, an constant integer, and a constant message string.
>
> > > All I need to do
> > > is pack my outgoing string and prepad its length in front of it.
>
> > But the code you posted prepended it with the native signed long
> > representation of 1. 1 is not the length of that message.
>
> The msgsnd docs are lying. The second argument of msgsnd should be the
> message, with a native long message type on the beginning; that is, the
> pack template 'l! a*' is correct, assuming your system doesn't pad after
> longs in structures. No length is required anywhere: msgsnd(2) takes a
> length argument, but perl can work this out from the length of the
> passed-in string.
>
> To the OP: why on earth aren't you using IPC::Msg? (Whose docs also lie,
> by the way: there is no need for a pack in the argument to ->snd.)
>
> Ben
This msgrcv function is getting to me..... Now that I got the length
in front of the string, I see the actual length. But if i send a
message greater than the size of SIZE value, the queue goes in a tizzy
and keeps trying to read the message again and over again. Whats the
point of having this length, If it can't do its job right.
|
|
Posted by unxl3arn3r on March 25, 2008, 1:53 pm
Please log in for more thread options
> Quoth xhos...@gmail.com:
>
>
>
> > > On Mar 24, 4:57 pm, xhos...@gmail.com wrote:
> > > > > Gurus
> > > > > I seem to be unable to pack a string padded with nulls and
> > > > > containing the length of the string prepended to it. This is my code
> > > > > snippet, where am i going wrong ?
>
> > > > > $message1 = pack("l! a*",1, "This is a test,This program is free
> > > > > software,you can redistribute it terms as Perl network, This is why I
> > > > > said to talk");
>
> > > > l! packs the "1", and a* packs the message. No where in there does the
> > > > length of the message enter into it.
>
> > > > I see that that is copied from the perldoc -f msgsnd. I can only
> > > > assume that that documentation is hosed.
>
> > > > Perhaps it should be this, instead?
>
> > > > pack("l! l!/a*",1,"foo")
>
> > > > Here l! packs the message type (1), l!/ packs the length of the
> > > > message, and a* packs the message itself.
>
> > > > > msgsnd($queue,$message1,0);
>
> > > why do I need to have the second variable in pack ?
>
> > I don't understand your question. None of the packs I've seen in this
> > thread are given two variables. They are given three constants--a constant
> > format string, an constant integer, and a constant message string.
>
> > > All I need to do
> > > is pack my outgoing string and prepad its length in front of it.
>
> > But the code you posted prepended it with the native signed long
> > representation of 1. 1 is not the length of that message.
>
> The msgsnd docs are lying. The second argument of msgsnd should be the
> message, with a native long message type on the beginning; that is, the
> pack template 'l! a*' is correct, assuming your system doesn't pad after
> longs in structures. No length is required anywhere: msgsnd(2) takes a
> length argument, but perl can work this out from the length of the
> passed-in string.
>
> To the OP: why on earth aren't you using IPC::Msg? (Whose docs also lie,
> by the way: there is no need for a pack in the argument to ->snd.)
>
> Ben
I forgot to put the code snippet in,
#!/usr/bin/perl
use strict;
use IPC::Msg;
use IPC::SysV;
#$outgoing=$ARGV[0];
my $key = 999;
my $queue = msgget($key,0) or die $!;
my $type = 1234;
#$queue = new IPC::Msg($key,0);
my $string = "This is a very long string The msgsnd docs are lying.
The second a
rgument of msgsnd should be the message, with a native long";
my $value = length ($string);
my $message1 = pack("l! a*",$value,$string);
print "one=$message1 \n";
msgsnd($queue,$message1,1);
print $! ;
receive
#!/usr/bin/perl
use IPC::SysV qw(IPC_RMID IPC_PRIVATE S_IRWXU IPC_CREAT
IPC_NOWAIT );
use IPC::Msg;
my $key = 999;
my $i = 0;
my $queue = msgget($key,&IPC_CREAT | 0777) or die $!;
my ($string, $i,$buffer,$type);
for ($i;$i < 15; $i++) {
if ( msgrcv($queue,$buffer,150,0,0) ) {
($type,$string)=unpack("l! a*",$buffer);
print $buffer .$string . "\n" . $type;
print "------------------------------\n";
} else {
print "Failed to read properly $! \n";
print "Length to be read = length($buffer) \n";
}
}
print "Now Removing\n";
msgctl($queue,IPC_RMID,0);
|
|
Posted by xhoster on March 25, 2008, 4:58 pm
Please log in for more thread options > >
> > The msgsnd docs are lying. The second argument of msgsnd should be the
> > message, with a native long message type on the beginning; that is, the
> > pack template 'l! a*' is correct, assuming your system doesn't pad
> > after longs in structures. No length is required anywhere: msgsnd(2)
> > takes a length argument, but perl can work this out from the length of
> > the passed-in string.
> >
> > To the OP: why on earth aren't you using IPC::Msg? (Whose docs also
> > lie, by the way: there is no need for a pack in the argument to ->snd.)
> >
> > Ben
...
>
> my $value = length ($string);
> my $message1 = pack("l! a*",$value,$string);
As Ben just explained, you should be packing the message type,
not the message length. So the original code was correct, despite
the documentation mis-describing it.
>
> if ( msgrcv($queue,$buffer,150,0,0) ) {
>
> This msgrcv function is getting to me..... Now that I got the length
> in front of the string, I see the actual length.
Where is it that you are you seeing the actual length?
> But if i send a
> message greater than the size of SIZE value, the queue goes in a tizzy
> and keeps trying to read the message again and over again.
It fails because that is what the system call msgrcv is documented to do
when the message is too big and when (msgflg & MSG_NOERROR) is 0. It "goes
into a tizzy" because that is what you coded it to do by wrapping a bizarre
loop around it. If you want it to truncate the message rather than
fail, then pass MSG_NOERROR rather than 0 as the flag to msgrcv.
> Whats the
> point of having this length, If it can't do its job right.
It does its jobs right. You just don't understand what its job is.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
|
|
Posted by unxl3arn3r on March 25, 2008, 5:08 pm
Please log in for more thread options On Mar 25, 4:58 pm, xhos...@gmail.com wrote:
>
> > > The msgsnd docs are lying. The second argument of msgsnd should be the
> > > message, with a native long message type on the beginning; that is, the
> > > pack template 'l! a*' is correct, assuming your system doesn't pad
> > > after longs in structures. No length is required anywhere: msgsnd(2)
> > > takes a length argument, but perl can work this out from the length of
> > > the passed-in string.
>
> > > To the OP: why on earth aren't you using IPC::Msg? (Whose docs also
> > > lie, by the way: there is no need for a pack in the argument to ->snd.)
>
> > > Ben
> ...
>
> > my $value = length ($string);
> > my $message1 = pack("l! a*",$value,$string);
>
> As Ben just explained, you should be packing the message type,
> not the message length. So the original code was correct, despite
> the documentation mis-describing it.
>
>
>
> > if ( msgrcv($queue,$buffer,150,0,0) ) {
>
> > This msgrcv function is getting to me..... Now that I got the length
> > in front of the string, I see the actual length.
>
> Where is it that you are you seeing the actual length?
>
> > But if i send a
> > message greater than the size of SIZE value, the queue goes in a tizzy
> > and keeps trying to read the message again and over again.
>
> It fails because that is what the system call msgrcv is documented to do
> when the message is too big and when (msgflg & MSG_NOERROR) is 0. It "goes
> into a tizzy" because that is what you coded it to do by wrapping a bizarre
> loop around it. If you want it to truncate the message rather than
> fail, then pass MSG_NOERROR rather than 0 as the flag to msgrcv.
>
> > Whats the
> > point of having this length, If it can't do its job right.
>
> It does its jobs right. You just don't understand what its job is.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.
Things would be easy to understand if there was a explanation of the
what flags were meant to do what. Even docs are crap, so I can't be
expected to master the perl msgrcv function without taking any help. I
misunderstood pack and Ben both. Going back to read it again.
|
|
Posted by xhoster on March 25, 2008, 6:09 pm
Please log in for more thread options >
> Things would be easy to understand if there was a explanation of the
> what flags were meant to do what. Even docs are crap, so I can't be
> expected to master the perl msgrcv function without taking any help. I
> misunderstood pack and Ben both. Going back to read it again.
Perl's msgrcv is just fairly shallow wrapper around the system call of the
same name. To understand the flags, you should read the system's
documentation ("man msgrcv"). But mastering it seems to be something of
dubious value, to me. If you need to make your Perl script talk to your C
programs that use this type of message passing, then you should already
know all about this stuff from the C perspective, which will make it much
easier to understand if from Perl as well. And if you are just talking
Perl to Perl, I certainly would pick a different (more Perlish) method to
do so.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
|
| Similar Threads | Posted | | strange output of pack in perl 5.8.0 | September 9, 2004, 7:36 pm |
| "RFC": re [un]pack() | September 10, 2004, 12:15 am |
| pack 'C3U*' not same as pack 'C3(xC)*' | June 23, 2005, 7:48 am |
| Need help with pack | October 1, 2005, 11:00 pm |
| pack and hex | October 3, 2008, 6:16 am |
| Trying to force 32-bit in pack() | October 12, 2004, 5:19 pm |
| pack and unpack ? | January 9, 2005, 11:37 am |
| pack problem? | August 27, 2006, 11:17 pm |
| sprintf or pack | January 30, 2007, 7:33 am |
| pack /unpack issue | November 10, 2005, 7:48 pm |
|