Networking between devices on the same wifi connection

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
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Networking between devices on the same wifi connection

Post by pauls313 »

Using LuaSocket Tutorial:Networking_with_UDP, how can I send packets from, say, my phone to my laptop? I've tried using 'localhost', ''127.0.0.1", "192.168.1.0" as addresses but none of these seem to work.
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Networking between devices on the same wifi connection

Post by keharriso »

You either need to make a note of your phone and laptop's IP addresses or use the broadcast address "255.255.255.255". "localhost" and "127.0.0.1" both refer to the local machine (your phone in this example).
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: Networking between devices on the same wifi connection

Post by pauls313 »

keharriso wrote: Thu Apr 11, 2019 8:43 pm You either need to make a note of your phone and laptop's IP addresses or use the broadcast address "255.255.255.255". "localhost" and "127.0.0.1" both refer to the local machine (your phone in this example).
Neither of these work. I've opened the 8090 port on my router but I still can't receive messages on both ends, both using their respective IPs and using 255.255.255.255

Could it have something to do with my router?
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Networking between devices on the same wifi connection

Post by grump »

pauls313 wrote: Thu Apr 11, 2019 8:59 pm Could it have something to do with my router?
Assuming that you're talking about an actual router, and not some kind of local firewall - probably not. The task of a router is to connect different networks. "Opening a port" on the router exposes your local network to the Internet for connections on that port, but it doesn't change anything about how local connections work, because a local network doesn't require routing by definition.
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Networking between devices on the same wifi connection

Post by keharriso »

How about you upload a .love of the most basic case you can make that doesn't work. There might be a problem in the way you're doing things that we have no way of knowing about.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: Networking between devices on the same wifi connection

Post by pauls313 »

Server:

Code: Select all

local socket = require "socket"
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('*', 12345)
local data, ip, port

function love.update(dt)
  
  data, ip, port = udp:receivefrom()
  
end

function love.draw()
  if data then
    love.graphics.print(data, 300, 300)
  end
end
Client:

Code: Select all

local socket = require "socket"
local address, port = "192.168.16.102", 12345
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)

function love.update(dt)
  udp:send("Hello!")
end
Attachments
client.love
(270 Bytes) Downloaded 252 times
server.love
(304 Bytes) Downloaded 264 times
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

Re: Networking between devices on the same wifi connection

Post by keharriso »

Try changing your server code to this:

Code: Select all

local socket = require "socket"
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('0.0.0.0', 12345)
local data, ip, port

function love.update(dt)
  local msg
  msg, ip, port = udp:receivefrom()
  if msg then
	data = msg
  end
end

function love.draw()
  if data then
    love.graphics.print(data, 300, 300)
  end
end
There are two changes:
1. The "*" address appears to be bugged on Windows, so use "0.0.0.0" instead.
2. Save the last received message to be displayed instead of overwriting it every update.
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: Networking between devices on the same wifi connection

Post by pauls313 »

keharriso wrote: Fri Apr 12, 2019 1:44 pm Try changing your server code to this:

Code: Select all

local socket = require "socket"
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('0.0.0.0', 12345)
local data, ip, port

function love.update(dt)
  local msg
  msg, ip, port = udp:receivefrom()
  if msg then
	data = msg
  end
end

function love.draw()
  if data then
    love.graphics.print(data, 300, 300)
  end
end
There are two changes:
1. The "*" address appears to be bugged on Windows, so use "0.0.0.0" instead.
2. Save the last received message to be displayed instead of overwriting it every update.
Thanks! The "*" seemed to be the problem.
Post Reply

Who is online

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