This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Windows10User's profile picture

Server doesn't respond to client

Started by Windows10User, 06 April 2018 - 07:42 AM
Windows10User #1
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:

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
Bomb Bloke #2
Posted 07 April 2018 - 01:47 AM
Merely opening a modem channel is not sufficient to start triggering incoming rednet message events. For those, you specifically need to tell the rednet API to open a modem.

http://www.computercraft.info/wiki/Rednet.open

Alternatively, you could look for modem_message events.

http://www.computercraft.info/wiki/Modem_message_(event)
Windows10User #3
Posted 07 April 2018 - 11:58 AM
Merely opening a modem channel is not sufficient to start triggering incoming rednet message events. For those, you specifically need to tell the rednet API to open a modem.

http://www.computerc...iki/Rednet.open

Alternatively, you could look for modem_message events.

http://www.computerc..._message_(event)

K, thanks. Haven't coded anything in CC for a long, long time, so will undoubtedly try it out.