Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Client 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)
- -- Function to read file content
- local function readFileContent(filePath)
- local file = fs.open(filePath, "r")
- if file then
- local content = file.readAll()
- file.close()
- return content
- end
- return nil
- end
- -- Prompt for server ID
- print("Enter server ID: ")
- local serverID = tonumber(read())
- -- Prompt for file details
- print("Enter file name: ")
- local fileName = read()
- -- Check if the file exists
- local filePath = shell.resolve(fileName)
- local fileExists = fs.exists(filePath)
- if not fileExists then
- print("File not found.")
- return
- end
- -- Get file size
- local fileSize = fs.getSize(filePath)
- -- Send file content to server with confirmation request
- local fileContent = readFileContent(filePath)
- if fileContent then
- local message = {
- fileName = fileName,
- fileSize = fileSize,
- fileContent = fileContent
- }
- rednet.send(serverID, message, "file_transfer")
- print("File sent to server.")
- -- Wait for confirmation response
- local senderID, confirmation, protocol = rednet.receive("confirmation")
- -- Display server's choice
- if confirmation and confirmation.status then
- print("Server's choice: " .. confirmation.status)
- else
- print("No confirmation received from server.")
- end
- end
Add Comment
Please, Sign In to add comment