Posted 10 May 2018 - 06:58 PM
I finally got my DNS and browser to work as they should. However, when the browser sends a request to the web server (to fetch the page), it doesn't work. The server receives the request for the page (the domain name + the name of the page) but doesn't seem to react to it and I know that because the browser won't stop waiting for the request!
Browser:
Web server:
P. S. automatic code indentation WHEN
Browser:
local modem = peripheral.find("modem")
if modem == nil then
error("No modem found!", 0)
end
if not modem.isWireless() then
error("Modem isn't wireless!")
end
print("Type the DNS ID:")
local dnschannel = tonumber(read())
print("Type the domain name:")
local domain = read()
print("Sending request. Press any key to cancel.")
modem.open(dnschannel)
modem.transmit(dnschannel, os.getComputerID(), string.match(domain, "[^/]+"))
local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
if msg == "FAIL" then
error("DNS error.", 0)
else
print(domain)
modem.transmit(tonumber(msg), os.getComputerID(), domain)
local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
shell.run("clear")
print("Contents:")
print(msg)
end
Web server:
local domain = "something.com"
local modem = peripheral.find("modem")
if modem == nil then
error("No modem found!", 0)
end
if not modem.isWireless() then
error("Modem isn't wireless!")
end
modem.open(os.getComputerID())
while true do
local event, mSide, sChannel, rChannel, msg, dist = os.pullEvent("modem_message")
if fs.exists(string.sub(msg, string.len(domain) + 2)) then
print(string.sub(msg, string.len(domain) + 2))
local fPage = fs.open(string.sub(msg, string.len(domain) + 2), "r")
local page = fPage.readAll()
fPage.close()
modem.transmit(rChannel, os.getComputerID(), page)
else
modem.transmit(rChannel, os.getComputerID(), "404 - Page Not Found")
end
end
P. S. automatic code indentation WHEN
Edited on 10 May 2018 - 04:59 PM