I found this code somewhere on this forum a long time ago. Not even sure it works to be honest but I recall testing it and it seemed to.
[lua]
require(“socket”)
local someRandomIP = “192.168.1.122” --This address you make up
local someRandomPort = “3102” --This port you make up
local mySocket = socket.udp() --Create a UDP socket like normal
--This is the weird part, we need to set the peer for some reason
mySocket:setpeername(someRandomIP,someRandomPort)
--I believe this binds the socket
--Then we can obtain the correct ip address and port
local myDevicesIpAddress, somePortChosenByTheOS = mySocket:getsockname()-- returns IP and Port
[/lua]