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()