[solved] Networking (UDP) - how to connect?

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
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

[solved] Networking (UDP) - how to connect?

Post by 4xEmpire »

Networking noob here, I have followed this https://love2d.org/wiki/Tutorial:Networking_with_UDP tutorial and it works just fine on my own PC with client and server running (localhost). What I do not fully understand is why I cannot get a client working on another PC with the server running on this PC.

To be clear: I have client running (as in the tutorial above) but the address variable is set to '192.[..blah blah...]' (my IPv4 address!). The server is running on my pc. They don't connect.

Is it a firewall issue? Am I naiively missing something?

Further context: I want to make a very very simple networked game so that down the line I can adapt my current game - a turn-based strategy - to allow networked battles.

Thank you for reading.
Last edited by 4xEmpire on Thu Aug 05, 2021 11:03 am, edited 1 time in total.
User avatar
ReFreezed
Party member
Posts: 323
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Networking (UDP) - how to connect?

Post by ReFreezed »

Are you using address "*" in the server? Is everyone using the same port?
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
User avatar
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

Re: Networking (UDP) - how to connect?

Post by 4xEmpire »

ReFreezed wrote: Wed Aug 04, 2021 6:29 pm Are you using address "*" in the server? Is everyone using the same port?
Thanks for replying
Yes, "*" in the server following the tutorial, and yes to the port as well.
User avatar
ReFreezed
Party member
Posts: 323
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Networking (UDP) - how to connect?

Post by ReFreezed »

Hmm, could you maybe share the relevant code?
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 2853
Joined: Sun Oct 18, 2015 2:58 pm

Re: Networking (UDP) - how to connect?

Post by pgimeno »

Are both PCs in a LAN?
User avatar
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

Re: Networking (UDP) - how to connect?

Post by 4xEmpire »

ReFreezed wrote: Wed Aug 04, 2021 6:50 pm Hmm, could you maybe share the relevant code?
The client is copy paste from the tutorial. The server was copied as well, but I adapted it to run in love2D format since it was just presented as pure lua code.

I appreciate any steer/insight you're able to give me :3


This is the client. (NB "localhost" below was replaced with the relevant IPv4 address)

Code: Select all

local socket = require 'socket'

local address, port = "localhost", 12345

local entity
local updaterate = 0.1

local world = {}
local t

function love.load()
    udp = socket.udp()

    udp:settimeout(0)

    udp:setpeername(address, port)

    math.randomseed(os.time())

    entity = tostring(math.random(99999))

    local dg = string.format("%s %s %d %d", entity, 'at', 320, 240)

    udp:send(dg)

    t = 0
end

function love.update(dt)
    t = t + dt

    if t>updaterate then
        local x,y = 0,0
        if love.keyboard.isDown('up') then y = y - (20*t) end
        if love.keyboard.isDown('down') then y = y + (20*t) end

        local dg = string.format("%s %s %f %f", entity, 'move', x, y)
        udp:send(dg)

        local dg = string.format("%s %s $", entity, 'update')
        udp:send(dg)

        t = t-updaterate
    end

    repeat
          data, msg = udp:receive()

          if data then
              ent, cmd, parms = data:match("^(%S*) (%S*) (.*)")

              if cmd == 'at' then
                  local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
                  assert(x and y)
          				x, y = tonumber(x), tonumber(y)
          				world[ent] = {x=x, y=y}
              else
      				    print("unrecognised command:", cmd)
              end
          elseif msg ~= 'timeout' then
      			   error("Network error: "..tostring(msg))
      		end
  	until not data

end

function love.draw()
      	for k, v in pairs(world) do
      		love.graphics.print(k, v.x, v.y)
      	end
end
This is the server.

Code: Select all

local socket = require "socket"
local udp
local world = {} -- the empty world-state
local data, msg_or_ip, port_or_nil
local entity, cmd, parms
local running = true
local ticker = 0

function love.load()
    udp = socket.udp()
    udp:settimeout(0)
    udp:setsockname('*', 12345)
end

function love.update(dt)
    ticker = ticker + dt
    if not running then
        if ticker>0.1 then
            running = true
        end
    end


    if running then
    	data, msg_or_ip, port_or_nil = udp:receivefrom()
    	if data then
    		-- more of these funky match paterns!
    		entity, cmd, parms = data:match("^(%S*) (%S*) (.*)")
    		if cmd == 'move' then
    			local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
    			assert(x and y) -- validation is better, but asserts will serve.
    			-- don't forget, even if you matched a "number", the result is still a string!
    			-- thankfully conversion is easy in lua.
    			x, y = tonumber(x), tonumber(y)
    			-- and finally we stash it away
    			local ent = world[entity] or {x=0, y=0}
    			world[entity] = {x=ent.x+x, y=ent.y+y}
    		elseif cmd == 'at' then
    			local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$")
    			assert(x and y) -- validation is better, but asserts will serve.
    			x, y = tonumber(x), tonumber(y)
    			world[entity] = {x=x, y=y}
    		elseif cmd == 'update' then
    			for k, v in pairs(world) do
    				udp:sendto(string.format("%s %s %d %d", k, 'at', v.x, v.y), msg_or_ip,  port_or_nil)
    			end
    		elseif cmd == 'quit' then
    			running = false;
    		else
    			print("unrecognised command:", cmd)
    		end
    	elseif msg_or_ip ~= 'timeout' then
    		error("Unknown network error: "..tostring(msg))
    	end

    	socket.sleep(0.01)
    end
end
User avatar
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

Re: Networking (UDP) - how to connect?

Post by 4xEmpire »

pgimeno wrote: Wed Aug 04, 2021 7:37 pm Are both PCs in a LAN?
No they are not, do they need to be for this to work? Sorry if its a silly question.
User avatar
ReFreezed
Party member
Posts: 323
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Networking (UDP) - how to connect?

Post by ReFreezed »

4xEmpire wrote: Wed Aug 04, 2021 8:37 pm No they are not, do they need to be for this to work?
If they aren't in the same network then you need to worry about port forwarding in the router that the server is connected to. Your clients also need to connect to the Internet-facing IP address - not the internal IP address in the LAN. IP addresses in the range 192.168.x.x are only used inside LANs.
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
User avatar
4xEmpire
Prole
Posts: 26
Joined: Wed Aug 04, 2021 4:49 pm

Re: Networking (UDP) - how to connect?

Post by 4xEmpire »

ReFreezed wrote: Wed Aug 04, 2021 8:51 pm
4xEmpire wrote: Wed Aug 04, 2021 8:37 pm No they are not, do they need to be for this to work?
If they aren't in the same network then you need to worry about port forwarding in the router that the server is connected to. Your clients also need to connect to the Internet-facing IP address - not the internal IP address in the LAN. IP addresses in the range 192.168.x.x are only used inside LANs.
Ok thanks I will look into that further.

For now I have experimented using the sock networking library (https://github.com/camchenry/sock.lua) and I was able to get the client to connect to the server using two different pcs in the same wlan.
Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests