Posted 06 April 2018 - 09:42 AM
So, I am trying to create a login server. When I enter the username and password on the client, it prints "Connecting to authentication server and checking…". Then it sends the username (which the server should check using its usernames table) and prints "Waiting for response from server…". When I go to the server computer, even though it should, it doesn't seem to react to the client. While it should print something like "Username received from "..id..", checking validity" and check the username, it's stuck on "Waiting for message". Please help!
Server:
Client:
Server:
os.pullEvent = os.pullEventRaw
local usernames = {"user1", "user2"}
local passwords = {"passwd1", "passwd2"}
local modem = peripheral.wrap("top")
local isFirstCycle = true
local channel = 999
function startUp()
print("This program is a server. As a results, it provides no interaction.")
print("Opening server on channel "..tostring(channel))
modem.open(channel)
end
while true do
if isFirstCycle then
startUp()
isFirstCycle = false
else
print("Waiting for message")
local id, msg, protocol = os.pullEvent("rednet_message")
if protocol == "username" then
print("Username received from "..id..", checking validity")
local valid = false
for i, v in ipairs(usernames) do
if msg == v then
valid = true
print("Username valid, waiting for password")
rednet.send(id, "VALID_USER", "INTERRUPT")
local id, msg, protocol = os.pullEvent("rednet_message")
if protocol == "password" then
if msg == passwords[i] then
print("Password valid, forwarding it to sender")
rednet.send(id, passwords[i], "VALID")
else
print("Password invalid, forwarding interrupt INVALID_PASS to sender")
rednet.send(id, "INVALID_PASS", "INTERRUPT")
end
end
end
end
if not valid then
print("Username not valid, forwarding interrupt INVALID_USER to sender")
rednet.send(id, "INVALID_USER", "INTERRUPT")
end
end
end
end
Client:
local user = ""
local pass = ""
local channel = 999
local modem = peripheral.find("modem")
modem.open(999)
print("Please enter your username")
user = read()
print("Please enter your password")
pass = read("*")
print("Connecting to authentication server and checking...")
rednet.send(2, user, "username")
print("Waiting for response from server...")
local id, msg, protocol = os.pullEvent("rednet_message")
if protocol == "INTERRUPT" then
if msg == "INVALID_USER" then
print("Invalid user name.")
sleep(2)
os.shutdown()
elseif msg == "VALID_USER" then
sleep(1)
rednet.send(2, pass, "password")
id, msg, protocol = os.pullEvent("rednet_message")
if protocol == "VALID" then
print("Access Granted!")
shell.exit()
elseif protocol == "INTERRUPT" then
if msg == "INVALID_PASS" then
print("Access Denied!")
sleep(2)
os.shutdown()
end
end
end
end