[31161] in Perl-Users-Digest
Perl-Users Digest, Issue: 2406 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun May 10 14:09:41 2009
Date: Sun, 10 May 2009 11:09:06 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Sun, 10 May 2009 Volume: 11 Number: 2406
Today's topics:
Finding domain and subdomains from host name <john1949@yahoo.com>
Re: Finding domain and subdomains from host name <noreply@gunnar.cc>
Re: Finding domain and subdomains from host name <unclebob@tnglwood.demon.co.uk>
Re: Finding domain and subdomains from host name <mvdwege_public@myrealbox.com>
Re: Finding domain and subdomains from host name <mvdwege_public@myrealbox.com>
Re: Finding domain and subdomains from host name <john1949@yahoo.com>
Re: Finding domain and subdomains from host name <mvdwege_public@myrealbox.com>
Re: Finding domain and subdomains from host name <john1949@yahoo.com>
Re: Finding domain and subdomains from host name <hjp-usenet2@hjp.at>
Re: Finding domain and subdomains from host name <john1949@yahoo.com>
Re: Finding domain and subdomains from host name <noreply@gunnar.cc>
Re: IO::Socket::INET on OSX or TCP stack problem <stuart@otenet.gr>
Re: IO::Socket::INET on OSX or TCP stack problem <uri@stemsystems.com>
new CPAN modules on Sun May 10 2009 (Randal Schwartz)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 10 May 2009 08:15:41 +0100
From: "John" <john1949@yahoo.com>
Subject: Finding domain and subdomains from host name
Message-Id: <gu5uum$87g$1@news.albasani.net>
Hi
The following will get me the IP address and then the hostname
use Socket;
my $ip=$ENV{'REMOTE_ADDR'};
my $host=gethostbyaddr(inet_aton($ip),AF_INET) ;
I now need the domain and subdomain names.
There should be something like this:
my @domains=???
but I cannot find it. Any ideas?
Regards
John
------------------------------
Date: Sun, 10 May 2009 11:02:17 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <76njhgF1d3tb2U1@mid.individual.net>
John wrote:
> The following will get me the IP address and then the hostname
>
> use Socket;
> my $ip=$ENV{'REMOTE_ADDR'};
> my $host=gethostbyaddr(inet_aton($ip),AF_INET) ;
>
> I now need the domain and subdomain names.
Try Net::DNS.
"Net::DNS is a collection of Perl modules that act as a Domain Name
System (DNS) resolver. It allows the programmer to perform DNS queries
that are beyond the capabilities of gethostbyname and gethostbyaddr."
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 10 May 2009 12:32:01 +0100
From: Robert Billing <unclebob@tnglwood.demon.co.uk>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <RWyNl.7849$oh6.3836@newsfe28.ams2>
John wrote:
> Hi
>
> The following will get me the IP address and then the hostname
>
> use Socket;
> my $ip=$ENV{'REMOTE_ADDR'};
> my $host=gethostbyaddr(inet_aton($ip),AF_INET) ;
>
> I now need the domain and subdomain names.
>
> There should be something like this:
>
> my @domains=???
>
> but I cannot find it. Any ideas?
This is what I have working on one of my websites:
# Subroutine to turn an IP address into a host name. Purists will
# point out that this is not completely reliable, but it is good
# enough for what we need.
sub host_name ($) {
use Socket;
my ( $ipaddr ) = @_ ;
my $p = pack ( "C4", split ( /\./, $ipaddr ) ) ;
( $name, undef, undef, undef, @addrs ) = gethostbyaddr ( $p,
AF_INET ) ;
return $name ;
}
--
I am Robert Billing, Christian, author, inventor, traveller, cook and
animal lover. "It burned me from within. It quickened; I was with book
as a woman is with child."
Quality e-books for portable readers: http://www.alex-library.com
------------------------------
Date: Sun, 10 May 2009 14:21:29 +0200
From: Mart van de Wege <mvdwege_public@myrealbox.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <86ljp5p2ra.fsf@gareth.avalon.lan>
"John" <john1949@yahoo.com> writes:
> Hi
>
> The following will get me the IP address and then the hostname
>
> use Socket;
> my $ip=$ENV{'REMOTE_ADDR'};
> my $host=gethostbyaddr(inet_aton($ip),AF_INET) ;
>
> I now need the domain and subdomain names.
>
> There should be something like this:
>
> my @domains=???
>
> but I cannot find it. Any ideas?
>
> Regards
> John
>
>
"John" <john1949@yahoo.com> writes:
> Hi
>
> The following will get me the IP address and then the hostname
>
> use Socket;
> my $ip=$ENV{'REMOTE_ADDR'};
> my $host=gethostbyaddr(inet_aton($ip),AF_INET) ;
>
> I now need the domain and subdomain names.
>
> There should be something like this:
>
> my @domains=???
>
> but I cannot find it. Any ideas?
>
Eh?
When I run that snippet, I get the fully qualified domain-name in
$host.
Try it like this:
#!/usr/bin/perl
use warnings;
use strict;
use Socket;
my $ip='174.133.238.154';
my $fqdn = gethostbyaddr(inet_aton($ip), AF_INET);
my ($hostname,@domainparts) = split(/\./,$fqdn);
print "hostname = $hostname\n";
print "domainparts = " . join(".", @domainparts) . "\n";
And substitute the ip address I used for the one you're looking up, of
course.
Of course, you're totally dependent on what your local C library
returns. If it returns a bare hostname, then this is not a Perl
problem.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Sun, 10 May 2009 14:22:39 +0200
From: Mart van de Wege <mvdwege_public@myrealbox.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <86hbztp2pc.fsf@gareth.avalon.lan>
Bugger.
Sorry about the copy/paste error.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Sun, 10 May 2009 15:11:26 +0100
From: "John" <john1949@yahoo.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <gu6naa$ar4$1@news.albasani.net>
"Mart van de Wege" <mvdwege_public@myrealbox.com> wrote in message
news:86hbztp2pc.fsf@gareth.avalon.lan...
>
> Bugger.
>
> Sorry about the copy/paste error.
>
> Mart
>
>
> --
> "We will need a longer wall when the revolution comes."
> --- AJS, quoting an uncertain source.
Yes, but this only returns the hostname.
The host name is, let's say, 91-187-0-3.something.com
I can pick out the IP address and the host name.
What I am seeking are the domains and subdomains located on that hostname.
I've looked at Net::DNS but I cannot find the answer there.
Regards
John
------------------------------
Date: Sun, 10 May 2009 16:25:24 +0200
From: Mart van de Wege <mvdwege_public@myrealbox.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <86ab5lox0r.fsf@gareth.avalon.lan>
"John" <john1949@yahoo.com> writes:
> "Mart van de Wege" <mvdwege_public@myrealbox.com> wrote in message
> news:86hbztp2pc.fsf@gareth.avalon.lan...
>>
>> Bugger.
>>
>> Sorry about the copy/paste error.
>>
>> Mart
>>
> Yes, but this only returns the hostname.
> The host name is, let's say, 91-187-0-3.something.com
That's not the hostname. Did you actually read my example? The naming of
my variables ought to have given you a clue.
> I can pick out the IP address and the host name. What I am seeking
> are the domains and subdomains located on that hostname. I've looked
> at Net::DNS but I cannot find the answer there.
If you don't understand DNS, then you can look all you want, but you are
*never* going to find the answer.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Sun, 10 May 2009 15:53:45 +0100
From: "John" <john1949@yahoo.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <gu6ppl$dv2$1@news.albasani.net>
"Mart van de Wege" <mvdwege_public@myrealbox.com> wrote in message
news:86ab5lox0r.fsf@gareth.avalon.lan...
> "John" <john1949@yahoo.com> writes:
>
>> "Mart van de Wege" <mvdwege_public@myrealbox.com> wrote in message
>> news:86hbztp2pc.fsf@gareth.avalon.lan...
>>>
>>> Bugger.
>>>
>>> Sorry about the copy/paste error.
>>>
>>> Mart
>>>
>
>> Yes, but this only returns the hostname.
>> The host name is, let's say, 91-187-0-3.something.com
>
> That's not the hostname. Did you actually read my example? The naming of
> my variables ought to have given you a clue.
>
>> I can pick out the IP address and the host name. What I am seeking
>> are the domains and subdomains located on that hostname. I've looked
>> at Net::DNS but I cannot find the answer there.
>
> If you don't understand DNS, then you can look all you want, but you are
> *never* going to find the answer.
>
> Mart
>
> --
> "We will need a longer wall when the revolution comes."
> --- AJS, quoting an uncertain source.
Many thanks. I did read your reply and I am grateful for it.
Just to make sure I was using the correct language I keyed in the IP address
to one of the IP/whois/DNS sites.
It came back as 91-187-0-3.something.com as the *hostname*
There would be several domains hosted on that one IP address other than the
domain something.com
and it was these I was seeking.
Regards
John
------------------------------
Date: Sun, 10 May 2009 17:54:07 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <slrnh0du4v.iqi.hjp-usenet2@hrunkner.hjp.at>
On 2009-05-10 14:11, John <john1949@yahoo.com> wrote:
> Yes, but this only returns the hostname.
> The host name is, let's say, 91-187-0-3.something.com
> I can pick out the IP address and the host name.
> What I am seeking are the domains and subdomains located on that hostname.
Domains and subdomains aren't "located on a hostname".
A (fully qualified) hostname is a domain name. Domains are nested, so
* the domain named "91-187-0-3.something.com." (which happens to be a host
name) is a subdomain of the domain named "something.com.",
* the domain named "something.com." is a subdomain of the domain named
"com.", and
* the domain named "com." is a subdomain of the domain named ".".
(the trailing dot in domain names is often omitted)
The domain "." is also called the "root domain". The domains directly
below it ("com.", "net.", "org.", "de.", "at.", ...) are called "top
level domains". The domains below that ("something.com." in your
example) are called second level domains, below that are the third level
domains, and so on.
hp
------------------------------
Date: Sun, 10 May 2009 18:02:10 +0100
From: "John" <john1949@yahoo.com>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <gu71ae$o6f$1@news.albasani.net>
"Peter J. Holzer" <hjp-usenet2@hjp.at> wrote in message
news:slrnh0du4v.iqi.hjp-usenet2@hrunkner.hjp.at...
> On 2009-05-10 14:11, John <john1949@yahoo.com> wrote:
>> Yes, but this only returns the hostname.
>> The host name is, let's say, 91-187-0-3.something.com
>> I can pick out the IP address and the host name.
>> What I am seeking are the domains and subdomains located on that
>> hostname.
>
> Domains and subdomains aren't "located on a hostname".
>
> A (fully qualified) hostname is a domain name. Domains are nested, so
> * the domain named "91-187-0-3.something.com." (which happens to be a host
> name) is a subdomain of the domain named "something.com.",
> * the domain named "something.com." is a subdomain of the domain named
> "com.", and
> * the domain named "com." is a subdomain of the domain named ".".
>
> (the trailing dot in domain names is often omitted)
>
> The domain "." is also called the "root domain". The domains directly
> below it ("com.", "net.", "org.", "de.", "at.", ...) are called "top
> level domains". The domains below that ("something.com." in your
> example) are called second level domains, below that are the third level
> domains, and so on.
>
> hp
Many thanks for that clarification.
But, my original problem which is to locate all domains using a single IP
address remains.
My recent searching throws up 'reverse IP' as maybe the term I should be
looking at to find all domains on a single IP.
Regards
John
------------------------------
Date: Sun, 10 May 2009 19:46:28 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Finding domain and subdomains from host name
Message-Id: <76oi8cF1dvoq3U1@mid.individual.net>
John wrote:
> But, my original problem which is to locate all domains using a single IP
> address remains.
> My recent searching throws up 'reverse IP' as maybe the term I should be
> looking at to find all domains on a single IP.
No. All domains resolve to an IP, but the other way around only works
accationally.
Take perl.org, for instance. Besides the second level domain, we know
there are a few third level domains, e.g. learn, dev, faq, lists...
$ host perl.org
perl.org has address 207.171.7.63
$ host 207.171.7.63
63.7.171.207.in-addr.arpa domain name pointer x3.develooper.com.
$
Honestly I believe that Google is your best bet. ;-)
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Sun, 10 May 2009 12:55:23 +0300
From: Stuart Gall <stuart@otenet.gr>
Subject: Re: IO::Socket::INET on OSX or TCP stack problem
Message-Id: <gu689o$lt4$1@mouse.otenet.gr>
On 2009-05-09 01:18:45 +0300, Uri Guttman <uri@stemsystems.com> said:
Hello uri,
Thank you for such a detailed reply.
I have placed some comments in line and included my new code at the
end, with most of your suggestions.
With your suggestions (I think mainly using sysread and syswrite) the
situation is much improved.
Before I would get a lockup after 4-10 loops now it goes for 17 - 20
That is a huge improvement, but I still eventualy get a bad packet
which is apparently not correctly handled by the TCP stack on OSX or by
perl on OSX I am not sure where the handoff is.
>>>>>> "SG" == Stuart Gall <stuart@otenet.gr> writes:
>
> SG> Good point, my code is so basic that I am convinced there must be a
> SG> generic problem on OSX. I just cant believe no one else has come
> SG> across it. :-(
>
> your code could still use major improvements.
>
>
> SG> I open the sockets with
> SG> sub OpenSocket() {
> SG> #Get Socket address for IP or open a new socket
> SG> my ($IP,$PORT,$S,$R,$I,@R);
> SG> $IP=shift;
>
> my( $ip ) = @_ ;
Like it.
>
> SG> $PORT=502;
>
> my $port = 502 ;
OK
>
> use lower case names for lexical vars. upper case is for constants (yes,
> the port is a constant but still i like lower as do most perl coders)
>
> SG> if(!$SOCKETS{$IP}) {
>
> unless( $sockets{$ip} ) {
Yes
>
> where is %sockets declared? it should be passed in via a hash ref
> instead of being a global
I did not include
sub CloseAllSockets
sub CloseSocket
%SOCKETS is a mutual static variable for those subs in a BEGIN block.
>
> SG> $S = new IO::Socket::INET (PeerAddr => $IP,
> SG> PeerPort => $PORT,
> SG> Proto => "tcp",
> SG> Timeout =>10,
> SG> Type => SOCK_STREAM) or return(undef);
>
> my $sock = IO::Socket::INET->new( "$ip:$port" ) ;
>
> there, isn't that much simpler? basic tcp socket connections can pass a
> single host:port string. the type, proto are defaulted
> correctly. timeout is fine with its default.
Perhaps, I prefer to specify specifically what I want, it is also
better when things are not working it is easier to tweak things.
>
> $sock or return ;
>
> no need to return undef. plain return does that when called in scalar
> context.
OK No need but when I am returning a value and undef on error I prefer
to write it explicitly.
>
> SG> $SOCKETS{$IP}=$S;
> SG> $SELECT->add($SOCKETS{$IP});
>
> what's with all the shouting? perl style is lower case
Well it wasn't working I thought if I shouted the computer might listen
to me a bit more :-))
>
> SG> $SOCKETS{$IP}->sockopt(TCP_NODELAY,1);
>
> here is the big question. why do you do that? it is not needed except in
> very special cases and your code is not one of them. it may even be the
> cause of the socket dropouts.
Well, I am communicating with real time devices using modbus, which has
short messages and I need an immediate reply. I do not want the short
messages buffered.
This line does not make any diferance to the issue I have. If it is
commented out I still have the same problem. I originaly put the line
in to try and fix the issue.
Actually I am not realy sure that this is the correct syntax.
with perl -w I get
Argument "TCP_NODELAY" isn't numeric in setsockopt at
/System/Library/Perl/5.8.6/darwin-thread-multi-2level/IO/Socket.pm line
247
Also all the packets (with data) sent from the mac have the PUSH flag
set even without TCP_NODELAY
I thought that TCP_NODELAY disables local buffering but should also set
PUSH so that the receiver does not buffer. Perhaps this is a feature of
the auto flush in IO::Socket, it sets PUSH if the packet is small and
being flushed.
>
> SG> };
>
> SG> return ($SOCKETS{$IP});
>
> why do you return the socket from the global hash? you have it in $S (or
> in my version $sock).
Answered above $SOCKETS is local static.
>
> SG> };
>
>
>
>
> SG> This is then called from for example
>
> SG> sub Read_10_Integer($$) {
> SG> my (@R, $IP, $ADD, $PORT,$COMMAND, $q, $socket, $len, @Reply);
>
> don't declare vars before you need them. you can my each of these in the
> assignments below
OK - better
> SG> $IP = shift;
> SG> $ADD = shift;
>
> my( $ip, $add ) = @_ ;
>
> shifting @_ for args is poor style. it is useful in some cases but
> assiging @_ to a list of my vars is standard perl style.
This is MUCH nicer, I have never seen it though! It is obvious now.
>
> SG> $PORT=502;
>
> why is this set again? if it is really a constant, move it outside the
> subs or pass it in as an arg.
It is left over from a time when I was opening the socket inside the
Read / write routine and immediate closing it.
I mentioned in my OP that I rewrote the code to keep the sockets open,
which then caused all my problems.
>
> SG> $ADD--;
>
> what is $ADD? use a better name for it.
OK $REGISTER
>
> SG> $COMMAND =
> SG> "\x00\x00\x00\x00\x00\x06\x01\x03".chr(int($ADD/256)).chr($ADD %
> SG> 256)."\x00\x0A";
>
>
> SG> unless($socket= OpenSocket($IP)) {
>
> since this is a boolean test, have OpenSocket just return 1 on success,
> not the socket itself.
I need the socket to read/write %SOCKET is static local
>
> SG> print "Can't connect ($IP) : $@ [$!]\n";
> SG> return undef;
> SG> };
>
> SG> print $socket $COMMAND;
>
> $socket->print( $COMMAND ) ;
>
> SG> $socket->read($r,6); #5th byte is the length byte
>
> $socket->sysread(my $read_buf, 6);
>
> use sysread on sockets. unless you are doing blocking line by line
> protos, you shouldn't use stdio or perlio for sockets.
I did not know that, this seems to have helped alot.
>
> SG> This is one place that it hangs with repeated retries on the tcpdump
> SG> @R=split(//,$r);
>
> my @read_bytes = split(//,$r);
>
> SG> return undef if(ord($R[0])!=0 or ord($R[1])!=0);
> SG> $len=ord($R[5]);
> SG> $socket->read($r,$len);
>
> maybe the retries are caused by the NO_DELAY thing. it moves data out in
> smaller packets and those are getting lost often. remove that unhelpful
> option and see what happens.
I put the option *IN* to try and correct the problem.
>
> SG> @R=split(//,$r);
> SG> Exception(ord($R[2])) if(ord($R[1]) > 0x80);
>
> SG> @Reply=();
>
> my @reply ;
>
> SG> for($q=0;$q<10;$q++) {
> SG> $Reply[$q]=ord($R[$q*2+3])*256+ord($R[$q*2+4]);
> SG> };
>
> i am sure that can be done more cleanly but i can't fathom it right now.
Probably, It is very C-ish and not very Perl-ish, but for sure it is
not the cause of the socket retry problems.
>
> uri
New code
BEGIN {
my %Sockets;
sub OpenSocket($) {
#Get Socket address for IP or open a new socket
my ($IP)=@_;
my $PORT=502;
unless ($Sockets{$IP}) {
my $S = new IO::Socket::INET (PeerAddr => $IP,
PeerPort => $PORT,
Proto => "tcp",
Timeout =>10,
Type => SOCK_STREAM) or return(undef);
$Sockets{$IP}=$S;
$SELECT->add($Sockets{$IP});
$Sockets{$IP}->sockopt(TCP_NODELAY,1);
};
return ($Sockets{$IP});
};
sub CloseAllSockets() {
foreach my $S (keys %Sockets) {
$Sockets{$S}->close();
delete $Sockets{$S};
};
$SELECT=IO::Select->new();
};
sub CloseSocket($) {
my($IP) = @_;
$SELECT->remove($SOCKETS{$IP});
$SOCKETS{$IP}->close();
delete $SOCKETS{$IP};
};
}# BEGIN BLOCK
And one of the calling functions
sub Read_10_Integer($$) {
my($IP,$Register) = @_;
$Register--;
#MBAP Header
# Bytes Meaning Value
# 2 Transaction ident - any number 00 00
# 2 Protocol Identifier - 0= Modbus 00 00
# 2 Length Number of following bytes 00 06
# 1 Unit Identifier 01
# 1 Function code 03 (Read Holding Register)
# 2 Register $REGISTER
# 2 Count 00 0A
my $COMMAND =
"\x00\x00\x00\x00\x00\x06\x01\x03".chr(int($Register/256)).chr($Register
% 256)."\x00\x0A";
my $socket = OpenSocket($IP);
unless($socket) {
print "Can't connect ($IP) : $@ [$!]\n";
return undef;
};
$socket->syswrite($COMMAND);
$socket->sysread(my $r,6); #5th byte is the length byte
my @R=split(//,$r);
return undef if(ord($R[0])!=0 or ord($R[1])!=0);
my $len=ord($R[5]);
$socket->sysread($r,$len);
@R=split(//,$r);
if(ord($R[1]) > 0x80) {
Exception(ord($R[2]),"READ 10 INTEGER IP=$IP ADD=$Register");
return(undef);
};
my @Reply=();
for(my $q=0;$q<10;$q++) {
$Reply[$q]=ord($R[$q*2+3])*256+ord($R[$q*2+4]);
};
return @Reply;
};
Stuart
------------------------------
Date: Sun, 10 May 2009 12:29:47 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: IO::Socket::INET on OSX or TCP stack problem
Message-Id: <873abcly4k.fsf@quad.sysarch.com>
>>>>> "SG" == Stuart Gall <stuart@otenet.gr> writes:
>>
SG> $S = new IO::Socket::INET (PeerAddr => $IP,
SG> PeerPort => $PORT,
SG> Proto => "tcp",
SG> Timeout =>10,
SG> Type => SOCK_STREAM) or return(undef);
>> my $sock = IO::Socket::INET->new( "$ip:$port" ) ;
>>
>> there, isn't that much simpler? basic tcp socket connections can pass a
>> single host:port string. the type, proto are defaulted
>> correctly. timeout is fine with its default.
SG> Perhaps, I prefer to specify specifically what I want, it is also
SG> better when things are not working it is easier to tweak things.
you won't need to tweak the connect call. almost no one ever does. as i
said, the protocol and type NEVER change if you want a tcp connection
and the timeout isn't critical to change.
>>
>> $sock or return ;
>>
>> no need to return undef. plain return does that when called in scalar
>> context.
SG> OK No need but when I am returning a value and undef on error I prefer
SG> to write it explicitly.
that is bad coding. if someone were to call your code in a list context,
instead of getting an empty list (which is false) they would get a
single element list containing undef (which is true). perl's return is
context sensitive and should be used with no arg for basic or empty returns.
SG> $SOCKETS{$IP}->sockopt(TCP_NODELAY,1);
>>
>> here is the big question. why do you do that? it is not needed except in
>> very special cases and your code is not one of them. it may even be the
>> cause of the socket dropouts.
SG> Well, I am communicating with real time devices using modbus, which
SG> has short messages and I need an immediate reply. I do not want the
SG> short messages buffered.
SG> This line does not make any diferance to the issue I have. If it is
SG> commented out I still have the same problem. I originaly put the line
SG> in to try and fix the issue.
it sounds like maybe your osx stack is crapola. but that isn't a perl
problem and i am sure many mac coders have written perl socket code for it.
SG> Actually I am not realy sure that this is the correct syntax.
SG> with perl -w I get
SG> Argument "TCP_NODELAY" isn't numeric in setsockopt at
SG> /System/Library/Perl/5.8.6/darwin-thread-multi-2level/IO/Socket.pm
SG> line 247
that is another problem. you didn't import the constant so it is being
passed as just a string. but i doubt you really need this.
SG> Also all the packets (with data) sent from the mac have the PUSH flag
SG> set even without TCP_NODELAY
SG> I thought that TCP_NODELAY disables local buffering but should also
SG> set PUSH so that the receiver does not buffer. Perhaps this is a
SG> feature of the auto flush in IO::Socket, it sets PUSH if the packet is
SG> small and being flushed.
autoflush has nothing to do with tcp. it is a stdio/perlio thing and if
you use syswrite it is ignored since that bypasses stdio.
>>
SG> };
>>
SG> return ($SOCKETS{$IP});
>>
>> why do you return the socket from the global hash? you have it in $S (or
>> in my version $sock).
SG> Answered above $SOCKETS is local static.
that doesn't answer why you return a hash lookup for the boolean
return. return 1 is simpler, faster and more accurate.
>>
SG> print "Can't connect ($IP) : $@ [$!]\n";
SG> return undef;
SG> };
>>
SG> print $socket $COMMAND;
>>
>> $socket->print( $COMMAND ) ;
>>
SG> $socket->read($r,6); #5th byte is the length byte
>>
>> $socket->sysread(my $read_buf, 6);
>>
>> use sysread on sockets. unless you are doing blocking line by line
>> protos, you shouldn't use stdio or perlio for sockets.
SG> I did not know that, this seems to have helped alot.
and don't mix sysread/write with other types of i/o on the sockets. that
will be living hell. always use one style or the others on any given
socket. the choice is also dependent on the data (not tcp) stream
protocol, whether you want blocking or not, how the other side is
working, etc. socket programming isn't a one sided thing.
>> maybe the retries are caused by the NO_DELAY thing. it moves data out in
>> smaller packets and those are getting lost often. remove that unhelpful
>> option and see what happens.
SG> I put the option *IN* to try and correct the problem.
and it didn't fix it so it isn't the cause. it would be unlikely to be
the cause since it is rarely used. and if your code really does work
fine on linux but not on osx, that points (but not prove) that the osx
stack is guilty. have you googled for osx and this error?
SG> BEGIN {
why the begin block? declaring empty vars subs in begin blocks does
nothing.
SG> my %Sockets;
SG> sub OpenSocket($) {
why are you using prototypes? they are meant for one thing alone
(changing how a sub call is parsed). they are not useful for arg
checking or stuff.
SG> #Get Socket address for IP or open a new socket
SG> my ($IP)=@_;
SG> my $PORT=502;
my $socket = $Sockets{$IP} ;
return $socket if $socket ;
the rest of the code is mainline, indented to the left and you save a
block. return as early when you can is a good style.
SG> unless ($Sockets{$IP}) {
SG> my $S = new IO::Socket::INET (PeerAddr => $IP,
SG> PeerPort => $PORT,
SG> Proto => "tcp",
SG> Timeout =>10,
SG> Type => SOCK_STREAM) or return(undef);
SG> $Sockets{$IP}=$S;
SG> $SELECT->add($Sockets{$IP});
SG> $Sockets{$IP}->sockopt(TCP_NODELAY,1);
you don't need that. and it isn't correct as you haven't defined the
constant. this means you aren't running strict/warnings clean. fix that
before you continue. let perl help you find bugs.
SG> sub CloseAllSockets() {
SG> foreach my $S (keys %Sockets) {
SG> $Sockets{$S}->close();
SG> delete $Sockets{$S};
values is useful there
foreach my $sock ( values %Sockets) {
$sock->close() ;
}
or even:
$_->close() for values %Sockets ;
SG> sub CloseSocket($) {
SG> my($IP) = @_;
SG> $SELECT->remove($SOCKETS{$IP});
SG> $SOCKETS{$IP}->close();
SG> delete $SOCKETS{$IP};
delete returns what it is deleting.
my $sock = delete $Sockets{$IP};
$SELECT->remove($sock);
$sock->close() ;
that saves you two hash lookups and is cleaner to read.
also you are using %SOCKETS and $Sockets which are not the same
var. strict would find that. use strict or die!!
SG> };
SG> }# BEGIN BLOCK
again, why the begin block? nothing i saw there needs to be done at
compile time.
SG> $socket->sysread(my $r,6); #5th byte is the length byte
SG> my @R=split(//,$r);
SG> return undef if(ord($R[0])!=0 or ord($R[1])!=0);
SG> my $len=ord($R[5]);
SG> $socket->sysread($r,$len);
SG> @R=split(//,$r);
SG> if(ord($R[1]) > 0x80) {
SG> Exception(ord($R[2]),"READ 10 INTEGER IP=$IP ADD=$Register");
SG> return(undef);
SG> };
SG> my @Reply=();
SG> for(my $q=0;$q<10;$q++) {
foreach my $q ( 0 .. 9 ) :
SG> $Reply[$q]=ord($R[$q*2+3])*256+ord($R[$q*2+4]);
SG> };
since you seem to need byte values use unpack to get them. much cleaner
than all those ord calls. same for building the command, pack is better
then chr calls. there is a doc called perlpacktut you can read about
pack/unpack.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Sun, 10 May 2009 04:42:30 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Sun May 10 2009
Message-Id: <KJEvqu.D9q@zorch.sf-bay.org>
The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN). You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.
App-MrShell-2.0200
http://search.cpan.org/~jettero/App-MrShell-2.0200/
do everything the mrsh commandline tool can do and more
----
CPAN-Mini-Visit-0.01
http://search.cpan.org/~adamk/CPAN-Mini-Visit-0.01/
A generalised API version of David Golden's visitcpan
----
Catalyst-Plugin-ErrorCatcher-0.0.2.1
http://search.cpan.org/~chisel/Catalyst-Plugin-ErrorCatcher-0.0.2.1/
Catch application errors and emit them somewhere
----
Chart-Clicker-2.26
http://search.cpan.org/~gphat/Chart-Clicker-2.26/
Powerful, extensible charting.
----
Chart-Clicker-2.27
http://search.cpan.org/~gphat/Chart-Clicker-2.27/
Powerful, extensible charting.
----
Chart-Clicker-2.28
http://search.cpan.org/~gphat/Chart-Clicker-2.28/
Powerful, extensible charting.
----
Class-Accessor-Deep-0.01-proper
http://search.cpan.org/~ksuri/Class-Accessor-Deep-0.01-proper/
Automated accessor generation for nested structures inside objects
----
Continuity-1.0
http://search.cpan.org/~awwaiid/Continuity-1.0/
Abstract away statelessness of HTTP, for stateful Web applications
----
DayDayUp-0.94
http://search.cpan.org/~fayland/DayDayUp-0.94/
good good study, day day up
----
Devel-Declare-0.005002
http://search.cpan.org/~flora/Devel-Declare-0.005002/
Adding keywords to perl, in perl
----
Directory-Deploy-0.001
http://search.cpan.org/~rkrimen/Directory-Deploy-0.001/
Create files and directories on disk
----
HTML-Truncate-0.17
http://search.cpan.org/~ashley/HTML-Truncate-0.17/
(beta software) truncate HTML by percentage or character count while preserving well-formedness.
----
MP3-Tag-1.10
http://search.cpan.org/~ilyaz/MP3-Tag-1.10/
Module for reading tags of MP3 audio files
----
Mail-Lite-0.1001
http://search.cpan.org/~davinchi/Mail-Lite-0.1001/
----
Math-GSL-0.19_03
http://search.cpan.org/~leto/Math-GSL-0.19_03/
Perl interface to the GNU Scientific Library (GSL)
----
Module-Release-Git-0.12_01
http://search.cpan.org/~bdfoy/Module-Release-Git-0.12_01/
Use Git with Module::Release
----
Module-Release-Git-0.12_02
http://search.cpan.org/~bdfoy/Module-Release-Git-0.12_02/
Use Git with Module::Release
----
Module-ScanDeps-0.90
http://search.cpan.org/~smueller/Module-ScanDeps-0.90/
Recursively scan Perl code for dependencies
----
MojoMojo-0.999029
http://search.cpan.org/~mramberg/MojoMojo-0.999029/
A Catalyst & DBIx::Class powered Wiki.
----
MojoX-Routes-AsGraph-0.002
http://search.cpan.org/~melo/MojoX-Routes-AsGraph-0.002/
Create a graph from a MojoX::Routes object
----
MojoX-Routes-AsGraph-0.003
http://search.cpan.org/~melo/MojoX-Routes-AsGraph-0.003/
Create a graph from a MojoX::Routes object
----
MojoX-Routes-AsGraph-0.04
http://search.cpan.org/~melo/MojoX-Routes-AsGraph-0.04/
Create a graph from a MojoX::Routes object
----
MooseX-Runnable-Fuse-0.01
http://search.cpan.org/~jrockway/MooseX-Runnable-Fuse-0.01/
implement a FUSE filesystem as a Moose class
----
MyCPAN-App-DPAN-1.18_04
http://search.cpan.org/~bdfoy/MyCPAN-App-DPAN-1.18_04/
----
MySQL-Sandbox-3.0.00
http://search.cpan.org/~gmax/MySQL-Sandbox-3.0.00/
Quickly installs MySQL side server, either standalone or in groups
----
Net-Google-AuthSub-0.5
http://search.cpan.org/~simonw/Net-Google-AuthSub-0.5/
interact with sites that implement Google style AuthSub
----
Net-Google-Calendar-0.97
http://search.cpan.org/~simonw/Net-Google-Calendar-0.97/
programmatic access to Google's Calendar API
----
Net-Redmine-0.05
http://search.cpan.org/~gugod/Net-Redmine-0.05/
A mechanized-based programming API against redmine server.
----
Padre-Plugin-Perl6-0.39
http://search.cpan.org/~azawawi/Padre-Plugin-Perl6-0.39/
Padre plugin for Perl6
----
Perl-Metrics2-0.02
http://search.cpan.org/~adamk/Perl-Metrics2-0.02/
Perl metrics storage and processing engine
----
RSSycklr-0.10
http://search.cpan.org/~ashley/RSSycklr-0.10/
(beta) Highly configurable recycling of syndication (RSS/Atom) feeds into tailored, guaranteed XHTML fragments.
----
Sepia-0.98
http://search.cpan.org/~seano/Sepia-0.98/
Simple Emacs-Perl Interface
----
SmokeRunner-Multi-0.15
http://search.cpan.org/~drolsky/SmokeRunner-Multi-0.15/
Manage smoke tests across multiple branches/checkouts/projects
----
Socket-Class-2.21
http://search.cpan.org/~chrmue/Socket-Class-2.21/
A class to communicate with sockets
----
Statistics-Descriptive-2.8
http://search.cpan.org/~shlomif/Statistics-Descriptive-2.8/
Module of basic descriptive statistical functions.
----
Syntax-Highlight-Perl6-0.54
http://search.cpan.org/~azawawi/Syntax-Highlight-Perl6-0.54/
Perl 6 Syntax Highlighter
----
Test-Continuous-0.65
http://search.cpan.org/~gugod/Test-Continuous-0.65/
Run your tests suite continusouly when developing.
----
Test-Continuous-0.66
http://search.cpan.org/~gugod/Test-Continuous-0.66/
Run your tests suite continusouly when developing.
----
Text-EditTranscript-0.04
http://search.cpan.org/~lmetcalf/Text-EditTranscript-0.04/
Perl extension for determining the edit transcript between two strings
----
Unicode-LineBreak-0.001
http://search.cpan.org/~nezumi/Unicode-LineBreak-0.001/
UAX #14 Unicode Line Breaking Algorithm
----
Unicode-LineBreak-0.001.510
http://search.cpan.org/~nezumi/Unicode-LineBreak-0.001.510/
UAX #14 Unicode Line Breaking Algorithm
----
WWW-Scraper-Yahoo360-0.03
http://search.cpan.org/~cosimo/WWW-Scraper-Yahoo360-0.03/
Yahoo 360 blogs old-fashioned crappy scraper
----
XML-Amazon-0.10
http://search.cpan.org/~hedwig/XML-Amazon-0.10/
Perl extension for getting information from Amazon
If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.
This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
http://www.stonehenge.com/merlyn/LinuxMag/col82.html
print "Just another Perl hacker," # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 2406
***************************************