Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet (INADDR_ANY)
by tye (Sage) on Jan 19, 2015 at 16:11 UTC
|
After perusing Net::Address::IP::Local, I think that perhaps you are getting back Socket::INADDR_ANY, which is likely "\0\0\0\0", which is a true value.
I expect that the next thing that your code does is to call Socket's inet_ntoa(). So you could also provide a default if that returns "0.0.0.0".
| [reply] [d/l] [select] |
|
|
I tried the code below but still it gives error :
use strict;
use warnings;
use Net::Address::IP::Local;
print ReturnIpAddress();
sub ReturnIpAddress
{
my $address;
select STDOUT;
eval{
$address = 'Net::Address::IP::Local'->public ; ## Rece
+iving the current IP ##
};
if ($@)
{
print "$@";
return '192.80.160.16'; ##If No IP address received th
+en return a static IP of order "192....."
}
else
{
return $address;
}
}
error is :
Unable to create UDP socket: A socket operation was attempted to an un
+reachab
network. at C:/Perl/site/lib/Net/Address/IP/Local.pm line 166.
| [reply] [d/l] [select] |
|
|
In the post you included in your other thread on the same subject, the output you gave showed the socket error and then the printed IP address, so that it appears that this part of your code is working properly, your program non longer dies on the error and is able to print the IP address after the socket open failure.
| [reply] |
|
|
Yes it gives some UDP socket connection couldnt created.. type of error. I dont have that in pc here. I saw in my school this morning. But error was linke some .. UDP socket cudnt create.. So how to handle it . I t gives this error because there is no internet access and even then i am trying to get IP , so no connection i guess thats why..
But how i can handle it ? Because if i print it like this :
print "adress is : 'Net::Address::IP::Local'->public";
Then it just print:
adress is :
Now it even do not print 0 or null so that i could habdle it like this if($adress=null) {do this} How to handle ? | [reply] [d/l] [select] |
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by vinoth.ree (Monsignor) on Jan 19, 2015 at 16:16 UTC
|
When i try to print that IP adress on disconnect then it prints nothing
Did you check Net::Address::IP::Local::Error exception is thrown?
| [reply] |
|
|
print "adress is : 'Net::Address::IP::Local'->public";
Then it just print:
adress is :
Now it even do not print 0 or null so that i could habdle it like this if($adress=null) {do this}
How to handle ? | [reply] [d/l] [select] |
|
|
use strict;
use warnings;
use Net::Address::IP::Local;
print ReturnIpAddress();
sub ReturnIpAddress
{
select STDOUT;
eval{
$address = 'Net::Address::IP::Local'->public ; ## Rece
+iving the current IP ##
};
if ($@)
{
print "$@";
return '192.80.160.16'; ##If No IP address received th
+en return a static IP of order "192....."
}
else
{
return $address;
}
}
| [reply] [d/l] |
|
|
|
|
|
|
|
|
If you’re really doing this, and it’s not just a typo, it’s not what you think. Package and object stuff does not work as interpolated strings. Well, not without trickery. This–
print "adress is : 'Net::Address::IP::Local'->public";
–will print this–
adress is : 'Net::Address::IP::Local'->public
| [reply] [d/l] [select] |
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by locked_user sundialsvc4 (Abbot) on Jan 19, 2015 at 17:38 UTC
|
The eval strategy should work, if the package is indeed throwing an exception (which eval will catch). But, notice that in the example code above, the value of $@ was printed. So, the message would appear to “still show up,” even though this time it’s coming from that print statement.
Add two different print statements, one to if part and a different one to the else part, so that you can clearly see which one was executed.
It is proper behavior for the package to have thrown an exception (i.e. die()) in a case where, due to not being connected to the Internet (and therefore having no “local IP-address” at all), no value could sensibly be returned. You just have to catch that exception, and eval should do that.
| |
|
|
Hii,
Thamks for the answer.
I get this error from that code :
Unable to create UDP socket: A socket operation was attempted to an un
+reachable
network....
And don't this static IP return handle this error ?
I mean doing this :
return $address='192.80.160.16';
should return return '192.80.160.16' instead of this error message. so how to get rid of this error and return '192.80.160.16' when there is no IP recived (when disconnected) otherwise return the real IP which it receives. | [reply] [d/l] [select] |
|
|
Just don't print $@ if you don't want this message to be printed.
| [reply] [d/l] |
|
|
Instead of using eval string, why don't you simply type it without all the extra quotes?
| [reply] |
Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet
by locked_user sundialsvc4 (Abbot) on Jan 20, 2015 at 00:42 UTC
|
I (still ...) strongly suspect that you are seeing the error-message because of print $@;. Therefore, I specifically suggest that you should modify the if/else structure in more or less this way:
if ($@) {
print STDERR "An exception was caught!\n";
# (set the default IP-address here, etc ...)
} else {
print STDERR "An exception did not occur.\n"
# ( grab the real address here)
}
print STDERR "... so the IP-address is $ipaddr\n";
# (etc. etc ...)
In other words, cause the code to explicitly log what it’s doing, one way or the other, to remove all doubt as to what it is doing.
| |