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

[Question] Password server

Started by thomas479, 18 August 2012 - 02:33 PM
thomas479 #1
Posted 18 August 2012 - 04:33 PM
Hi,

I am trying to make a program and it doesn't do what I want it to do. I am a bit confused why it doesn't work.
The redstone output wont go on.
The program is a door lock, with a client computer and an server to check the password.

Here is the client code:

rednet.open("top")
local serverId = 12
while true do
write("Pass:")
local input = read("*")
  rednet.send(serverId, input)
senderId,message,distance = rednet.receive()
print("Binnenkomend wachtwoord")
if message == "ok" then do
  redstone.setOutput("left", true)
  sleep(2)
  redstone.setOutput("left", false)
end
if message == "nok" then do
print("Fout wachtwoord!")
end
end
end
end

And this is the server code:

local password = pass
local clientId = 10
rednet.open("right")
print("Online")
while true do
senderId,message,distance = rednet.receive()
print("Login poging")
if senderId == clientId then do
if message == password then do
  rednet.send(11, ok)
end
end
end
end
end

I can see that the client accesses the server, but the server does not communicate back.
Or my client code is so crappy I can't see it communicate back :(/>/>

I hope someone can explain what I am doing wrong!

Thomas
altalus #2
Posted 18 August 2012 - 04:54 PM
This line:

rednet.send(11, ok)

Could be the problem since you answer to computer 11 only. Might be better to answer to the computer that asked for a check ?


rednet.send(senderId, "ok") -- Do not forget the quotes, this is a string !

Also, you never send a "nok" message, so if the information are not right, the client never knows it right ?
thomas479 #3
Posted 18 August 2012 - 05:08 PM
Thanks for replaying!

I changed the code to send to the client that asked for a check.

And the nok message was I forgotten to delete… :(/>/> Woops.

But it is still not working…
I changed some things in the code, so I past it here again:

Client:

rednet.open("top")
local serverId = 12
local side = "left"
while true do
term.clear() -- Clears the screen
term.setCursorPos(1,1) -- Fixes the cursor position
write("Pass:")
local input = read("*")
term.clear() -- Clears the screen
term.setCursorPos(1,1) -- Fixes the cursor position
  rednet.send(serverId, input)
senderId,message,distance = rednet.receive()
print("Binnenkomend wachtwoord")
if message == "ok" then do
  redstone.setOutput(side, true)
  sleep(2)
  redstone.setOutput(side, false)
end
else
print("Fail")
end

end

And the server:


local password = pass
local clientId = 10
rednet.open("right")
print("Online")
while true do
senderId,message,distance = rednet.receive()
print("Login poging")
print(message)
if senderId == clientId then do
if message == password then do
  rednet.send(senderId, "ok")
end
end
end
end
end

Hope someone will spot my fails! :)/>/>

And I spotted my fail, I will write it very small because it is so incredible stupid… :)/>/>

In the server code it checks of the client id is 10. but the client is computer 11. Face palm…… :wub:/>/>
Edited on 18 August 2012 - 03:14 PM
altalus #4
Posted 18 August 2012 - 06:34 PM
Humm a quick thing I did not see before that might need to be corrected:

local password = pass -- this means the variable password = the variable pass

Might need to be:


local password = "pass" -- this means the variable password = the string "pass"

For debugging, what I suggest is to print it all on the server at each communication received like this for example:


while true do
  senderId,message,distance = rednet.receive()
  write ("Login poging :: ")
  print("sender: "..senderId.."; message: "..message..";")

  if senderId == clientId then do
	if message == password then do
	  rednet.send(senderId, "ok")
	end -- if
  end -- if
end -- while
Noodle #5
Posted 19 August 2012 - 04:55 PM
And the server:

local password = pass
local clientId = 10
rednet.open("right")
print("Online")
while true do
senderId,message,distance = rednet.receive()
print("Login poging")
print(message)
if senderId == clientId then do
if message == password then do
  rednet.send(senderId, "ok")
end
end
end
end
end
You don't need all the ends. You only need to end the statements.