ComputerCraft Forums

ComputerCraft => Programs => APIs and Utilities => Topic started by: MegaEmailman on Apr 23, 2019, 05:24 AM

Title: File Request and Server
Post by: MegaEmailman on Apr 23, 2019, 05:24 AM
I have made two simple scripts here. One sets up a file server, the other requests files from that server. I would suggest adding the RequestServer script to your computer's startup file for the easiest use.

FileRequest
local args = {...}
print("What side is your modem on?")
side = io.read()
rednet.open(side)
print("Usage:")
print("FileRequest (ComputerID) (Filename)")
if #args ~= 2 then
print("The program requires two arguments. You gave "..#args..".")
end
if #args == 2 then
id = tonumber(args[1])
rednet.send(id, "REQUEST")
rednet.send(id, args[2])
id,msg = rednet.receive()
if msg == "RECEIVE" then
id2,msg2 = rednet.receive()
f = fs.open(args[2], "w")
f.write(msg2)
f.close()
print("File "..args[2].." successfully requested.")
end
end

RequestServer
print("What side is your modem on?")
side = io.read()
rednet.open(side)
while true do
id,msg = rednet.receive()
if msg == "REQUEST" then
id2,msg2 = rednet.receive()
rednet.send(id, "RECEIVE")
if id2 == id then
f = fs.open(msg2, "r")
rednet.send(id, f.readAll()
f.close()
end
end
end