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

Sending files via RedNet?

Started by xXThunder607Xx, 20 April 2014 - 10:19 PM
xXThunder607Xx #1
Posted 21 April 2014 - 12:19 AM
For quite a while now, I've been wondering how you can send whole files via a rednet modem. I have seen this done in the past, and I'm sure it can be done. I was told to use the FileSystems API, however I couldn't seem to do it. If someone could please help me with this that would be awesome.
Edited on 21 April 2014 - 12:43 AM
RoD #2
Posted 21 April 2014 - 12:48 AM
Google is your friend.
I had made one program for this. Really basic actually:

it sends a message (file title)
then it sends another (file itself)

the other computer listens the title, creates a file with that title, then when it receives the file content it writes the content to the file.

Give it a try, test new things :)/>
Bomb Bloke #3
Posted 21 April 2014 - 01:08 AM
Perhaps have a read through this thread.
apemanzilla #4
Posted 21 April 2014 - 01:49 AM
Currently, I'm in a competition to see who can make the best ComputerCraft program, and I'm stuck. I really need to know how to send whole programs via RedNet. I was told to use the filesystems API, however I didnt find anything there of-use. If someone could please tell me how this would be done, that would be fantastic. Thanks.
The fs API is EXACTLY what you need, it lets you open and read a file into a string which can be sent via rednet. Additionally, this belongs in the Ask a Pro section.
RoD #5
Posted 21 April 2014 - 10:27 AM
this thread is good and complete i think, just like the thread that bomb bloke posted but i wanted to share :)/>
DannySMc #6
Posted 21 April 2014 - 11:55 PM
Simples…
You will need a file to send and two computers with modems!

First of all you need to open the file to see its contents:

local file = fs.open("File_name", "r")
file = file.readAll()
Explained: You use local so it isn't made global, and then you use fs.open (used to open up a file/create one) to open the file, you then give it the arguments: the file name and what mode (w = write mode, r = read mode, a = append mode), in this case we need read mode. Then you use the handle: "file" and then use the readAll command to view the contents. using "file = " first stops it from displaying the opened data.

Now you need to serialize the content to a string:

strData = textutils.serialize(file)
Explained: Now I have named the new contents as "strData", I have used "str" before hand as it is better so I know what the actual variable contains. The textutils API allows you to manipulate text. Serialize will convert a table of data to a string. The argument is "file" as that is where all the contents of the program we opened earlier is contained.

Now you are ready to send it to another computer! First of all we need to set up a receiving computer! This is literally the reverse so here is the code:

-- First open up the modem
rednet.open("modem_side") -- Modem_side is where the modem is
local intID, strMessage = rednet.receive() -- rednet api to allow incoming messages (optional argument which is a timeout to wait)
fileContents = textutils.unserialize(strMessage) -- where the string of the file is stored
local file = fs.open("file_name", "w") -- write mode
file.write(fileContents) -- write file
file.close() -- close it

Now you need top open up the modem on the sending computer and then send it:

rednet.open("modem_side") -- modem_side being where the modem is positioned
rednet.send(intID, strData) -- the send function

Now the intID on the sending computer needs to be the ID of the computer that has the receiving code on it.
This can be obtained by typing "id" (without quotes) in the command line.

Sending Computer Code:

local file = fs.open("example", "r")
file = file.readAll()
fileContents = textutils.serialize(file)
rednet.open("top")
rednet.send(2, fileContents)

Receiving Computer Code:

local intID, strMsg = rednet.receive()
fileContents = textutils.unserialize(strMsg)
local file = fs.open("example", "w")
file.write(fileContents)
file.close()
RoD #7
Posted 22 April 2014 - 05:34 PM
Simples…
You will need a file to send and two computers with modems!

First of all you need to open the file to see its contents:

local file = fs.open("File_name", "r")
file = file.readAll()
Explained: You use local so it isn't made global, and then you use fs.open (used to open up a file/create one) to open the file, you then give it the arguments: the file name and what mode (w = write mode, r = read mode, a = append mode), in this case we need read mode. Then you use the handle: "file" and then use the readAll command to view the contents. using "file = " first stops it from displaying the opened data.

Now you need to serialize the content to a string:

strData = textutils.serialize(file)
Explained: Now I have named the new contents as "strData", I have used "str" before hand as it is better so I know what the actual variable contains. The textutils API allows you to manipulate text. Serialize will convert a table of data to a string. The argument is "file" as that is where all the contents of the program we opened earlier is contained.

Now you are ready to send it to another computer! First of all we need to set up a receiving computer! This is literally the reverse so here is the code:

-- First open up the modem
rednet.open("modem_side") -- Modem_side is where the modem is
local intID, strMessage = rednet.receive() -- rednet api to allow incoming messages (optional argument which is a timeout to wait)
fileContents = textutils.unserialize(strMessage) -- where the string of the file is stored
local file = fs.open("file_name", "w") -- write mode
file.write(fileContents) -- write file
file.close() -- close it

Now you need top open up the modem on the sending computer and then send it:

rednet.open("modem_side") -- modem_side being where the modem is positioned
rednet.send(intID, strData) -- the send function

Now the intID on the sending computer needs to be the ID of the computer that has the receiving code on it.
This can be obtained by typing "id" (without quotes) in the command line.

Sending Computer Code:

local file = fs.open("example", "r")
file = file.readAll()
fileContents = textutils.serialize(file)
rednet.open("top")
rednet.send(2, fileContents)

Receiving Computer Code:

local intID, strMsg = rednet.receive()
fileContents = textutils.unserialize(strMsg)
local file = fs.open("example", "w")
file.write(fileContents)
file.close()
Actually you dont need to serialize it, i did this without serializing and it worked :P/>