Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to open the modem on the target computer
- local function openModem()
- local modemSide
- for _, side in ipairs(rs.getSides()) do
- if peripheral.getType(side) == "modem" then
- modemSide = side
- break
- end
- end
- if modemSide and not rednet.isOpen(modemSide) then
- rednet.open(modemSide)
- print("Modem opened on side: " .. modemSide)
- end
- end
- -- Function to receive a file from the source computer
- local function receiveFile(targetPath, contents)
- local file = fs.open(targetPath, "w")
- if file then
- file.write(contents)
- file.close()
- print("Received file: " .. targetPath)
- else
- print("Error: Unable to save file " .. targetPath)
- end
- end
- -- Main function to handle file transfer
- local function main()
- openModem()
- -- Main loop to listen for incoming files
- while true do
- local senderID, message, protocol = rednet.receive("fileTransfer")
- -- Check if the message is a file transfer request
- if type(message) == "table" and #message == 2 then
- local targetPath = message[1]
- local contents = message[2]
- receiveFile(targetPath, contents)
- end
- end
- end
- -- Execute the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement