DOGGYWOOF

RECOVERY CLIENT

Jul 9th, 2024
4
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. -- Client Program
  2.  
  3. -- Function to find and return the modem peripheral
  4. local function findModem()
  5. for _, side in ipairs(peripheral.getNames()) do
  6. if peripheral.getType(side) == "modem" then
  7. return side
  8. end
  9. end
  10. return nil
  11. end
  12.  
  13. local modemSide = findModem()
  14. if not modemSide then
  15. print("Error: No modem found. Please attach a modem.")
  16. return
  17. end
  18.  
  19. rednet.open(modemSide)
  20.  
  21. -- Function to read file content
  22. local function readFileContent(filePath)
  23. local file = fs.open(filePath, "r")
  24. if file then
  25. local content = file.readAll()
  26. file.close()
  27. return content
  28. end
  29. return nil
  30. end
  31.  
  32. -- Prompt for server ID
  33. print("Enter server ID: ")
  34. local serverID = tonumber(read())
  35.  
  36. -- Prompt for file details
  37. print("Enter file name: ")
  38. local fileName = read()
  39.  
  40. -- Check if the file exists
  41. local filePath = shell.resolve(fileName)
  42. local fileExists = fs.exists(filePath)
  43. if not fileExists then
  44. print("File not found.")
  45. return
  46. end
  47.  
  48. -- Get file size
  49. local fileSize = fs.getSize(filePath)
  50.  
  51. -- Send file content to server with confirmation request
  52. local fileContent = readFileContent(filePath)
  53. if fileContent then
  54. local message = {
  55. fileName = fileName,
  56. fileSize = fileSize,
  57. fileContent = fileContent
  58. }
  59.  
  60. rednet.send(serverID, message, "file_transfer")
  61. print("File sent to server.")
  62.  
  63. -- Wait for confirmation response
  64. local senderID, confirmation, protocol = rednet.receive("confirmation")
  65.  
  66. -- Display server's choice
  67. if confirmation and confirmation.status then
  68. print("Server's choice: " .. confirmation.status)
  69. else
  70. print("No confirmation received from server.")
  71. end
  72. end
  73.  
Add Comment
Please, Sign In to add comment