How do I send files to a client?

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
GoldenTCat
Prole
Posts: 7
Joined: Thu Dec 26, 2024 12:48 am
Location: Brazil

How do I send files to a client?

Post by GoldenTCat »

Hello, I'm doing the multiplayer part of my game and the way it works is a little difficult for me, the client and the server contain the "resources" folder but the server has this folder full, while the client has it empty, in this every time a client connects, the server would send the resource files to the client.
Of course this would only happen once when entering the server, but I don't know how to do it because I don't understand how "lua-enet" or even "socket" works, could someone make a function that sends these files and one to receive it? because I've been trying for days. :(

server main.lua

Code: Select all

require("listen")
local enet = require("enet")
local ip, port = "localhost", 12345
host = enet.host_create(ip..":"..port)

function love.load()
	print("running Server/main.lua")
end
function love.update(dt)
	event = host:service(100)
	if event then
		serverCommands(host, event)
	end
	
end
client main.lua

Code: Select all

require("send")
local enet = require("enet")
local ip, port = "localhost", 12345
client = enet:host_create()lis
server = client:connect(ip..":"..port)
	
function love.load()
	print("running Client/main.lua")
end
function love.update(dt)
	event = client:service(100)
	if event then
		clientCommands(client, server, event)
	end
end
listen.lua

Code: Select all

function serverCommands(host, event)
	dt = love.timer.getDelta()
	
	if event.type == "receive" then
		print("Got message: ", event.data, event.peer)
		event.peer:send(tostring("test"))
	elseif event.type == "connect" then
		print(event.peer, "connected.")
	elseif event.type == "disconnect" then
		print(event.peer, "disconnected.")
	end
end
send.lua

Code: Select all

function clientCommands(client, server, event)
	dt = love.timer.getDelta()
	
	event.peer:ping()
	if event.type == "receive" then
		print("Got message: ", event.data, event.peer)
		event.peer:send("ping")
	elseif event.type == "connect" then
		print(event.peer, "connected.")
		event.peer:send("ping")
	elseif event.type == "disconnect" then
		print(event.peer, "disconnected.")
	end
end
User avatar
dusoft
Party member
Posts: 765
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: How do I send files to a client?

Post by dusoft »

You need to encode such file for sending by using one of the algos such as base64 or more efficient ones (and/or pack it).

You don't mention whether you have successful *message* communication or not.

Your pseudocode would be:
  1. open file
  2. encode it
  3. send it
  4. receive file (the other end)
  5. decode it
  6. save it
  7. (optionally confirm receipt)
See the tutorial here:
https://love2d.org/wiki/Tutorial:Networking_with_UDP

Also check these libraries:
https://love2d.org/wiki/Networking
https://github.com/love2d-community/awe ... networking
GoldenTCat
Prole
Posts: 7
Joined: Thu Dec 26, 2024 12:48 am
Location: Brazil

Re: How do I send files to a client?

Post by GoldenTCat »

dusoft wrote: Mon Jan 06, 2025 7:55 pm You need to encode such file for sending by using one of the algos such as base64 or more efficient ones (and/or pack it).

You don't mention whether you have successful *message* communication or not.

Your pseudocode would be:
  1. open file
  2. encode it
  3. send it
  4. receive file (the other end)
  5. decode it
  6. save it
  7. (optionally confirm receipt)
See the tutorial here:
https://love2d.org/wiki/Tutorial:Networking_with_UDP

Also check these libraries:
https://love2d.org/wiki/Networking
https://github.com/love2d-community/awe ... networking
I'll give it a read and I'll try until it works, thanks :ultrahappy: .
GoldenTCat
Prole
Posts: 7
Joined: Thu Dec 26, 2024 12:48 am
Location: Brazil

Re: How do I send files to a client?

Post by GoldenTCat »

Error: sock.lua:287: attempt to index local 'message' (a number value)
stack traceback:
[love "boot.lua"]:352: in function '__index'
sock.lua:287: in function '__unpack'
sock.lua:249: in function 'update'
main.lua:27: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'
I had this error when I tested sock.lua.
this error appeared in the server's main.lua.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 4 guests