Getting local IP address using UDP?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: Getting local IP address using UDP?

Post by yal2du »

togFox wrote: Fri Apr 22, 2022 10:51 pm It is only one line of native love code:

local socket = require 'socket' -- socket is native to LOVE but needs a REQUIRE
local HOST_NAME = socket.dns.gethostname()
local HOST_IP_ADDRESS = socket.dns.toip(HOST_NAME)
That works well if the machine has a single network interface configured; just note that difficulties may arise if more than one adapter is (or has been) present. For example, one windows box that used to have a physical cable connection returns the ip for that nic even though the only valid network is now a wifi usb adapter. And my virtual box based linux test system returns the (debian style) loopback ip of 127.0.1.1 and not the adapter address set by dhcp.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Getting local IP address using UDP?

Post by ReFreezed »

yal2du wrote: Fri Apr 22, 2022 7:29 pm

Code: Select all

      local s_dg = " Default Gateway . . . . . . . . . :"
      local s_ip = " IPv4 Address. . . . . . . . . . . : "
Note that this will only work on an English installation of Windows as these texts are translated.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: Getting local IP address using UDP?

Post by yal2du »

ReFreezed wrote: Sat Apr 23, 2022 2:34 am
yal2du wrote: Fri Apr 22, 2022 7:29 pm

Code: Select all

     -- Nuttin' but engrish stringz
Note that this will only work on an English installation of Windows as these texts are translated.
System locale could be an issue with any os and non-ascii encoding formats could be a problem as well (lua 5.1 string library doesn't handle anything beyond 8-bit clean). What I posted was just an approach to importing the output of a shell command, and parsing the same (mainly to illustrate that it is possible to do so without having to write/read to/from a temporary file). A temp file approach (using os.execute and possibly os.tempname) can be cumbersome if one must poll to check if/when a file has been modified).

Ideally, using the luajit.ffi one could make the appropriate system call and grab all the current legit ip addresses and avoid all of this 'hackiness'.

Hackiness - “Hacky” problem-solving behavior. We construe hacky problem-solving behavior as a two-facet construct that relates to the extent to which one invests cognitive resources in order to avoid tedious/brute-force problem-solving strategies in favor of clever/finessed problem-solving strategies. The first facet of the construct is intolerance of tedium, and refers to the threshold of tedium at which one chooses to seek a hacky solution to a problem. The second facet of the construct is tenacity, and refers to the amount of time and effort one will expend in order to find a hacky solution before one “gives up” and defers to a brute-force solution. ~ Zack Tidler
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests