Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Server Program
- -- Function to find and return the modem peripheral
- local function findModem()
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "modem" then
- return side
- end
- end
- return nil
- end
- local modemSide = findModem()
- if not modemSide then
- print("Error: No modem found. Please attach a modem.")
- return
- end
- rednet.open(modemSide)
- local saveDirectory = "DGOS"
- local serverID = os.getComputerID()
- -- Create save directory if it doesn't exist
- if not fs.exists(saveDirectory) then
- fs.makeDir(saveDirectory)
- end
- -- Function to read the target client ID from file
- local function readClientID()
- local path = "/Network/Recovery.ID"
- if fs.exists(path) then
- local file = fs.open(path, "r")
- local id = tonumber(file.readAll())
- file.close()
- return id
- else
- return nil
- end
- end
- local clientID = readClientID()
- if not clientID then
- print("Error: No client ID found in /Network/Recovery.ID")
- return
- end
- -- Main loop to receive messages
- while true do
- local senderID, message, protocol = rednet.receive()
- if protocol == "file_transfer" then
- print("Received file request from ID: " .. senderID)
- if type(message) == "table" and message.fileName and message.fileSize then
- print("File Name: " .. message.fileName)
- print("File Size: " .. message.fileSize .. " bytes")
- -- Automatically accept the file transfer
- local filePath = fs.combine(saveDirectory, message.fileName)
- local file = fs.open(filePath, "w")
- file.write(message.fileContent)
- file.close()
- rednet.send(senderID, { status = "Accepted" }, "confirmation")
- print("File transfer accepted and saved to " .. filePath)
- else
- print("Invalid message received.")
- rednet.send(senderID, { status = "Invalid file transfer request" }, "confirmation")
- end
- else
- print("Received unknown protocol message.")
- rednet.send(senderID, { status = "Unknown protocol" }, "confirmation")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement