Posted 14 January 2018 - 07:27 PM
(I'm reposting this because I didn't know about the "code" and "spoiler" tags, sorry. I'll be more careful next time.)
I've been troubleshooting my FTP_RECEIVER and FTP_SENDER programs for a while, but I'm stuck on a bug that keeps freezing the computer on the initialisation stage. Any help is appreciated, whether it's just to say I have the wrong approach, or if it's a solution to my problem. I'm kind of a beginner to this, although I have some coding experience in other languages including this one.
I start FTP_RECEIVER first, because then it will continue waiting for the next computer, FTP_SENDER to respond. The code is below
FTP_RECEIVER:
FTP_SENDER:
Thank you for reading, and for your patience.
I've been troubleshooting my FTP_RECEIVER and FTP_SENDER programs for a while, but I'm stuck on a bug that keeps freezing the computer on the initialisation stage. Any help is appreciated, whether it's just to say I have the wrong approach, or if it's a solution to my problem. I'm kind of a beginner to this, although I have some coding experience in other languages including this one.
I start FTP_RECEIVER first, because then it will continue waiting for the next computer, FTP_SENDER to respond. The code is below
FTP_RECEIVER:
Spoiler
-- Open rednet and clear the terminal
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
-- Inform user
print("-------------------------------")
print(" Receiving files ")
print("-------------------------------")
-- While loop to continually perform task until the file has been received
while true do
-- Fancy loading text =3
term.setCursorPos(17,2)
textutils.slowPrint("...")
-- Broadcasts a "ready" message
rednet.broadcast("Ready for files! Waiting for a second...")
-- Waits for initialisation prompt
id,msg = rednet.receive(1)
term.setCursorPos(17,2)
print(" ")
-- If statement to filter out possible noise
if msg == "Sending..." then
-- Informs user that the File Transfer has been initialised
term.setCursorPos(1,4)
print(" Initialised...")
print("-------------------------------")
-- Sends confirmation message in case it was not received"
rednet.send(id,"Got the initialisation message! Now waiting...")
-- Waits for file name and then prepares file
id2,msg2 = rednet.receive()
if msg2 == "Sending..." then
print(" Oh... got an error. Error 04")
break
else
file = fs.open(msg2,"w")
-- Confirmation message
rednet.send(id,"Got file name...")
-- Waits for file contents
id3,msg3 = rednet.receive()
-- If statement to test for an intercepter
if id3 == id and msg3 == "TERMINATE" then
print("TERMINATING FTP, ASSUMING ATTEMPT AT INTERCEPT")
-- Deletes file to remove clutter
fs.delete(msg2)
rednet.send(id3,"FTP has been terminated by original sender.")
break
else
-- If there is no intercepter (that the computer knows of), continues file transfer
file.write(msg3)
print("Got the file! Now ending FTP...")
print("-------------------------------")
break
end
end
else
-- Don't do anything, just return to the beginning of the loop.
end
end
FTP_SENDER:
Spoiler
-- Open rednet and clear the terminal
rednet.open("top")
term.clear()
term.setCursorPos(1,1)
repeat
-- Prompt the user
print("--------------------------------------")
print(" Specify the file you want to send... ")
print("--------------------------------------")
term.setCursorPos(2,4)
file = io.read()
print("--------------------------------------")
print(" Who should this be sent to? (ID)")
print("--------------------------------------")
term.setCursorPos(2,8)
ID = io.read()
print("--------------------------------------")
textutils.slowPrint(" Sending file...")
print("--------------------------------------")
-- Make sure that ID is a number
if tonumber(ID) == nil then
print("The ID you gave is not a number! Please be more careful, try again.")
else
IDN = tonumber(ID)
end
until IDN
-- Loop until finished
while true do
-- Waits for ready message
id,msg = rednet.receive(1)
-- Make sure the file exists
if fs.exists(file) == true then
-- Set variable for the file's contents
contents = fs.open(file, "r")
-- Filters out possible noise
if msg == "Ready for files! Waiting for a second..." then
print(" Got ready message! Initialising...")
print("--------------------------------------")
-- Sends initialisation prompt
rednet.send(IDN,"Sending...")
-- Waits for confirmation message
id2,msg2 = rednet.receive(2)
if msg == "Got the initialisation message! Now waiting..." then
-- Sends file name to recipient
rednet.send(IDN,file)
id3,msg3 = rednet.receive(2)
-- Checks the message and ID
if msg3 == "Got file name..." and id3 == IDN then
-- If all is good, continue and finish
rednet.send(IDN,contents)
print(" File sent!")
print("--------------------------------------")
sleep(2)
print("Finished successfully")
sleep(2)
break
elseif id2 ~= IDN then
-- If not all is good, send the "TERMINATE" command and finish
rednet.send(IDN,"TERMINATE")
print("TERMINATING FTP, ASSUMING ATTEMPT AT INTERCEPT")
sleep(2)
print("Finished with error code 02: TERMINATE")
break
else
end
else
-- Do nothing
end
else
-- Do nothing
end
else
-- If the file doesn't exist, end program.
print(file.." does not exist, or the wrong computer responded. =(")
sleep(2)
term.setTextColor(colors.red)
print("Finished with error code 01: File does not exist")
break
end
end
term.setTextColor(colors.white)
Thank you for reading, and for your patience.