Advertisement
ElijahCrafter

Untitled

Mar 19th, 2024 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. -- Function to open the modem on the target computer
  2. local function openModem()
  3.     local modemSide
  4.     for _, side in ipairs(rs.getSides()) do
  5.         if peripheral.getType(side) == "modem" then
  6.             modemSide = side
  7.             break
  8.         end
  9.     end
  10.  
  11.     if modemSide and not rednet.isOpen(modemSide) then
  12.         rednet.open(modemSide)
  13.         print("Modem opened on side: " .. modemSide)
  14.     end
  15. end
  16.  
  17. -- Function to receive a file from the source computer
  18. local function receiveFile(targetPath, contents)
  19.     local file = fs.open(targetPath, "w")
  20.     if file then
  21.         file.write(contents)
  22.         file.close()
  23.         print("Received file: " .. targetPath)
  24.     else
  25.         print("Error: Unable to save file " .. targetPath)
  26.     end
  27. end
  28.  
  29. -- Main function to handle file transfer
  30. local function main()
  31.     openModem()
  32.  
  33.     -- Main loop to listen for incoming files
  34.     while true do
  35.         local senderID, message, protocol = rednet.receive("fileTransfer")
  36.         -- Check if the message is a file transfer request
  37.         if type(message) == "table" and #message == 2 then
  38.             local targetPath = message[1]
  39.             local contents = message[2]
  40.             receiveFile(targetPath, contents)
  41.         end
  42.     end
  43. end
  44.  
  45. -- Execute the main function
  46. main()
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement