[SOLVED] Luasocket UDP broadcast

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.
Post Reply
User avatar
AdrianN
Citizen
Posts: 73
Joined: Wed Mar 28, 2018 5:13 pm
Location: Lima

[SOLVED] Luasocket UDP broadcast

Post by AdrianN »

Hi, I'm trying to make a list of local servers for my game, like this.

Image

https://gamedev.stackexchange.com/quest ... -their-lan

Searching in internet, I found could do it with broadcast.

https://stackoverflow.com/questions/281 ... -datagrams

I've adapted a code that I found and it works well, but when I use broadcast option it only works for me with localhost
Using two computers win10 with disabled firewall I had the same result.

Server code

Code: Select all

local Class= require "libs.hump.class"
local socket = require "socket"
local mime = require "mime"

local servidor_alterno = Class{}

function servidor_alterno:init(ip)
	self.udp_server = socket.udp()
	self.udp_server:setsockname("192.168.0.255", "22121")
	
	self.udp_server:setoption('broadcast',true)
	self.udp_server:setoption('dontroute',true)

	self.udp_server:settimeout(0)

	self.ip_value = ip
end

function servidor_alterno:update_alterno(dt)
	data, ip, port = self.udp_server:receivefrom()
    if data == "Unirse" then

	   local datos = ...

	   local data_encode = mime.b64(datos)
        self.udp_server:sendto(data_encode, ip, port)
    end
end

return servidor_alterno
Client code

Code: Select all

function cliente_alterno:init()
	self.udp_cliente = socket.udp()

	self.udp_cliente:setpeername("192.168.0.255", "22121")
	self.udp_cliente:setoption('broadcast',true)
	self.udp_cliente:setoption('dontroute',true)
	self.udp_cliente:settimeout(0)

	self.registro_server={}


end

function cliente_alterno:update_alterno(dt)
	self.udp_cliente:send("Unirse")
	data = self.udp_cliente:receive()
	if data then
	    local data_normal = mime.unb64(data)
	    ....
	end
end
I don't have much knowledge of this lib, maybe I'm wrong about something.

Edit:
I found what I needed

https://love2d.org/forums/viewtopic.php ... 9&start=20

Change 255.255.255.255 broadcast to 192.168.0.255
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 48 guests