B4J Code Snippet Get your WAN address in 3 lines

Discussion in 'B4J Code Snippets' started by Daestrum, May 30, 2015.

  1. Daestrum

    Daestrum Well-Known Member Licensed User

    Ever wanted to know your wan IP address ?
    add the jNashorn library and add all these lines.
    Dim nash As jNashorn ' dont forget to include jNashorn Library
    nash.evalString($"var myip;    load('http://l2.io/ip.js?var=myip');"$)
    or using jScriptEngine library (system javascript engine if you don't have Nashorn)
    Dim js As jScriptEngine ' dont forget to include jScriptEngine Library
    js.evalString($"var myip;    load('http://l2.io/ip.js?var=myip');"$)
    Log("JScriptEngine >>" & js.engineGet("myip"))
    (java 8)
    Last edited: May 30, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

    You need to be careful with such code as it will block the main thread. If the connection is slow your whole app will freeze (a single request can take 30 seconds or more in some cases).

    I recommend you to use HttpUtils2 instead.
  3. Daestrum

    Daestrum Well-Known Member Licensed User

    True, it was just to show the 'http://l2.io/ip.js' returns your wan ip. It's more for a utility than production program.
    That said, I don't think it can be done without a scriptengine as it relies on the fact it runs javascript.
    Last edited: May 31, 2015
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice