Client/Server UDP within a thread.
Posted: Tue Feb 09, 2021 9:54 pm
Hi, lurked but haven't posted nor created an account until now, I've been just playing around with love for a while getting to grips with it, I've tried to setup a BASIC server with UDP that listens only. I have a client that on a keypress it sends a single word, "fish". I've tested the thread push and popping standalone with the server sections in and it worked fine when tested so I'm assuming that I'm doing it correctly. It's got to be something wrong with the UDP code, I'm literally just wanting to send a single word. Can you help me, even if it's just a sanity check? I'm on mac OSX, MacBook pro Air with Big Sur, not sure if that changes anything in regards to issues.
I haven't got a .love, so will post the code.
Client: main.lua
Server: main.lua
Server: listen.lua
Many Thanks, FD!
I haven't got a .love, so will post the code.
Client: main.lua
Code: Select all
local socket = require("socket")
local ipaddr = "192.168.1.71"
local udp = socket.udp()
local sendit
function love.load()
end
function sendMessage(message)
udp:sendto(message, ipaddr, 65000)
end
function love.keypressed(key)
if key == 'g' then
sendit = not sendit
end
end
function love.update(dt)
if sendit then
sendMessage('fish')
end
end
function love.draw()
local delta = love.timer.getAverageDelta()
-- Display the frame time in milliseconds for convenience.
-- A lower frame time means more frames per second.
love.graphics.print(string.format("Average frame time: %.3f ms", 1000 * delta), 0, 0)
fps = love.timer.getFPS( )
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 0, 15)
love.graphics.print("Client: "..ipaddr, 0, 35)
-- local count = love.thread.getChannel('status'):getCount()
-- if count then
-- love.graphics.print(count, 0, 100)
-- end
-- local stat = love.thread.getChannel('status'):peek()
-- if stat then
-- love.graphics.print("test: "..tostring(stat), 0, 80)
-- end
if sendit then
love.graphics.print("sending",50,50)
end
end
Code: Select all
local socket = require("socket")
function love.load()
serverThread = love.thread.newThread("listen.lua")
serverThread:start()
local err = serverThread:getError()
address = "Server Address: " .. socket.dns.toip(socket.dns.gethostname())
end
function love.keypressed(key)
if key == 's' then
elseif key == 'q' then
love.event.quit(exitstatus)
elseif key == 'r' then
love.event.quit( "restart" )
end
end
function love.update(dt)
local message = love.thread.getChannel('messages'):peek() --set to peek not pop so the first sent it shown
local ip = love.thread.getChannel('ips'):peek()
local port = love.thread.getChannel('ports'):peek()
if message then
elseif ip ~= 'timeout' then
error("Unknown network error: "..tostring(msg))
end
assert( not err, error )
end
function love.draw()
local delta = love.timer.getAverageDelta()
-- Display the frame time in milliseconds for convenience.
-- A lower frame time means more frames per second.
love.graphics.print(string.format("Average frame time: %.3f ms", 1000 * delta), 0, 0)
fps = love.timer.getFPS( )
love.graphics.print("Current FPS: "..tostring(love.timer.getFPS( )), 0, 15)
love.graphics.print(address, 0, 35)
--see if were getting any messages on the thread channel
local count = love.thread.getChannel('messages'):getCount()
if count then
love.graphics.print(count, 0, 100)
end
if message then
love.graphics.print("Message: "..tostring(message), 0, 60)
elseif ip then
love.graphics.print("IP: "..tostring(ip), 0, 75)
elseif port then
love.graphics.print("Port: "..tonumber(port), 0, 99)
end
end
Code: Select all
local socket = require("socket")
function listen()
local udp = socket.udp()
udp:setsockname ('*', 65000)
udp:settimeout(0)
while true do
local message, ip, port = udp:receivefrom()
love.thread.getChannel('messages'):push(message)
love.thread.getChannel('ips'):push(ip)
love.thread.getChannel('ports'):push(port)
socket.sleep(0.01)
end
end
listen()