in reply to How to handle Net::Address::IP::Local'->public when we are not connected to internet

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".

- tye        

  • Comment on Re: How to handle Net::Address::IP::Local'->public when we are not connected to internet (INADDR_ANY)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How to handle Net::Address::IP::Local'->public when we are not connected to internet (INADDR_ANY)
by Anonymous Monk on Jan 19, 2015 at 17:28 UTC
    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.
      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.

      Je suis Charlie.
Re^2: How to handle Net::Address::IP::Local'->public when we are not connected to internet (INADDR_ANY)
by ppp (Acolyte) on Jan 19, 2015 at 16:40 UTC
    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 ?