This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
spook811's profile picture

LUA - File Host

Started by spook811, 27 August 2012 - 02:21 PM
spook811 #1
Posted 27 August 2012 - 04:21 PM
Hi, id like to have a computer which hosts multiple files that any other computer can read only. So random computer conects to file hosting server and reads the file wanted. How do I get rednet to open that file? Does the host just need to be rednet.open?

thanks,
spook811
ardera #2
Posted 27 August 2012 - 07:00 PM
No, you have to code a program for this, and I think it CAN be a bit complicated…
funkey100 #3
Posted 27 August 2012 - 08:35 PM
I can do that just what do you mean by read only? Do you mean it can't edit the file in the server or it can't edit the file once it's received?
spook811 #4
Posted 27 August 2012 - 08:51 PM
I dont want people to be able to just edit the file, it has to be read only to people connecting.
funkey100 #5
Posted 27 August 2012 - 09:11 PM
It depends what type of file it is. If it's a program you can put it to run and then delete itself when it's done being used, assuming you won't need to save that program. What type of file is it? Text file, program ect
spook811 #6
Posted 27 August 2012 - 09:40 PM
it would be a text file.
funkey100 #7
Posted 27 August 2012 - 09:54 PM
If you don't need to save it, just read it, you can make a program that receives it without saving and then displays it. If you need to save the text file, then I think it's impossible.
Grim Reaper #8
Posted 27 August 2012 - 10:01 PM
You can have people only be able to read the file, however they may edit if they alter your program to do so. However, they won't be able to send the edited version back to the server unless your server program is written to accept other files, but that doesn't seem like what you want.

If you write your program to only send the textual data and the client only reads it then you should be safe. Like I stated previously people may edit your program so the text is written into a file, but they can't send it back.
spook811 #9
Posted 27 August 2012 - 10:04 PM
ok thanks, ill have a play with coding that tomorrow, may pop on for a little extra with the coding.

Thanks,
Spook811
Jan #10
Posted 27 August 2012 - 10:16 PM
Wrote this little code for you and it tested it:

Server:

rednet.open("right")
print("I am hosting. ID: "..os.getComputerID())
while true do
local sender,mes = rednet.receive()
local cmd = mes:sub(1,2)
local filename = mes:sub(3)
if cmd=="ge" then
if fs.exists(filename) then
  local handle = io.open(filename,"r")
  rednet.send(sender,"fi"..handle:read("*a"))
  handle:close()
else
  rednet.send(sender,"er".."file does not exist")
end
end
end

Receiver: program name: redget

rednet.open("right")
local xArgs = { ... }
if xArgs[2]==nil then
print("Syntax: redget <id> <filename>")
else
rednet.send(tonumber(xArgs[1]),"ge"..xArgs[2])
local sender,mes=rednet.receive(1)
local cmd=mes:sub(1,2)
local filedata = mes:sub(3)
if mes==nil then
print("Took too long")
elseif cmd=="fi" then
local handle=io.open(xArgs[2],"w")
handle:write(filedata)
handle:close()
print("done!")
else
print("Something went wrong")
end
end

Note this is a quick-and-dirty program, it is not secure, and doesnt work good with sub-folders.