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

Rednet file transfer problem?

Started by DannySMc, 19 July 2014 - 05:54 PM
DannySMc #1
Posted 19 July 2014 - 07:54 PM
Okay so I was making a program, and all it did was open a file and send it to the other computer. The thing is it worked all the time… Until the recent update… So here is the code for the program that will open the file and send it:
EDIT: The program I am making is part of a file server…


rednet.open("back")
print("> ID: 21")
while true do
  local args = { os.pullEvent() }
  if args[1] == "rednet_message" then
	print("> Incoming ID: "..args[2])
	local file = fs.open("DaFile", "r")
	file = file.readAll()
	file = textutils.serialize(file)
	rednet.send(24, file)
	print("> Done!")
  end
end
now this will wait for any message as long as it is a rednet message.

Here is the code that send the request and then downloads it and saves it:

rednet.open("top")
rednet.send(21, "give_me_da_file_biatch!")
local arg1, arg2 = rednet.receive()
print("> Server ID: "..arg1)
file = textutils.unserialize(arg2)
local f = fs.open("DaFile", "w")
f:write(file)
f:close()
print("> done!")

Now I tried doing it without the:

file = file.readAll()
but it said it cannot serialize a type?
so I added it in and the file saves and everything but the file doesn't contain the data, it contains:

{}
So i really don't know what I am doing wrong :/
Edited on 19 July 2014 - 05:55 PM
AssossaGPB #2
Posted 19 July 2014 - 09:52 PM
Your problem is here:

file = file.readAll()
file = textutils.serialize(file)
this should be:

local data = file.readAll()
data = textutils.serialize(data)
Bomb Bloke #3
Posted 20 July 2014 - 01:23 AM
Er, not exactly.

While overwriting the file handle in the variable "file" with the content read from the actual file is definitely a bad idea (because it prevents you from closing that handle), it's not behind the symptoms described. Those are caused by trying to serialise/unserialise data that already started out as serialised and should remain so.

rednet.open("back")
print("> ID: 21")
while true do
  local args = { os.pullEvent() }
  if args[1] == "rednet_message" then
        print("> Incoming ID: "..args[2])
        local file = fs.open("DaFile", "r")
        rednet.send(24, file.readAll())
        file.close()
        print("> Done!")
  end
end

rednet.open("top")
rednet.send(21, "give_me_da_file_biatch!")
local arg1, arg2 = rednet.receive()
print("> Server ID: "..arg1)
local file = fs.open("DaFile", "w")
file.write(arg2)
file.close()
print("> done!")