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

[Solved] Help getting server/client consoles working (in-game)

Started by XenoG, 13 August 2012 - 10:09 AM
XenoG #1
Posted 13 August 2012 - 12:09 PM
So I've got a console that I'd like to run as a server, and several other consoles that I'd run as a client. Each have modems connected, and I've checked all ID's of all computers. The idea is you log in to a computer and then it opens the door (A more complex variation of the password-protected doors). The problem is after I've got the server up and running, and the client up and running, the client will always give me a "Not Authorised" message (an error message that I write client-side).

If anyone could tell me why I'm getting the error message that'd be perfect. Below is the code, let me know if I need to provide any more details. Also, the Client doesn't have any door-opening code in it, because that code is quite trivial and easy to do.

Eventually it'll be set up so that certain doors have permission levels, so only Administrators can enter the server room, etc.

Server:

term.clear() --Clear any text
term.setCursorPos(1,1) --Set cursor position to beginning
write"This is not a user-accessible computer." -- User information
write"Log in at one of the USER PCs." --User information
local firstCycle = true -- Used to determine whether to run bootUp function. Boolean variable
local validSender = false --Used to determine whether valid information has been entered. Boolean variable
local modemSide = "right" --Determines modem side. String variable
local valid = false --Used to determine whether check is made as to whether to return password or not. Boolean variable
users = {{uname = "XenoG", pword = "pl455ey"},
   {uname = "Test", pword = "user"},
   {uname = "Xenogene", pword = "lolgasm"}
   } --Users table. Left is key, right is value. In effect Username-Password (Key-Value)
senders = {612, 613, 617, 618, 619, 621} --Table of all computer ID's that can access server.
function bootUp() --New function "bootUp()"
rednet.open(modemSide) --Sends message to rednet to make sure the modem is open
end --End function
while true do --Main server loop
validSender = false --set validSender to false. Needs to be done every loop round

if firstCycle then --If the this is the first cycle then
  bootUp() --Call the bootUp() function
end --End IF statement

senderId, message, distance = rednet.receive() --rednet.receive() function call. Loop waits here until a message is received
for i,v in ipairs(senders) do --For index, value, search in the table senders, then execute next line
  if v == senderId then --If v is equal to a senderId (found in senders table) then
   validSender = true --Set validSender to true
   break --Break the loop
  end --End IF statement
end --End FOR loop

if validSender then --If validSender is true, execute next line
  for k,v in pairs(users) do --For index, value, search in the table users, then execute next line
   if message == k then --If k is equal to message (found in users table) then
	valid = true --Set valid to true
	password = users[v.value] -- Set password to the value of v
   else --Otherwise
	valid = false --Set valid to false
   end --End IF statement
  end --End FOR loop

  if valid then --If valid is true, execute next line
   rednet.send(senderId, password) -- Send the senderID and the password to rednet
  else --Otherwise
   rednet.send(senderId, "Not Valid") --Send a Not Valid message to rednet
  end --End IF statement
end --End IF statement
end --End WHILE TRUE DO loop

Client code:

local locker = true
local attempted_login = true
local password_server = 17 -- change to the ID of your password server computer
rednet.open("left") -- change to the side your rednet modem is on
while locker do
attempted_login = false
term.clear()
term.setCursorPos(1,1)
print("Welcome to a USERS PC : Roaming Profile Enabled")
print("What would you like to do?")
print("[1] Login (*)")
print("[2] Shutdown")
write("> ")
local input = read()
if input == "2" then
  os.shutdown()
elseif input == "1" then
  attempted_login = true
  print("Please login...")
  write("Username: ")
  local username = read()
  write("Password: ")
  local password = read("*")
  rednet.send(password_server, username)
  senderId, message, distance = rednet.receive(5)
  if password == message then
   locker = false
   term.clear()
   term.setCursorPos(1,1)
   print("Welcome ", username)
  else
   print("Not authorised.")
   sleep(1)
  end
else
  print("Command not recognised...")
  sleep(2)
end
end
BigSHinyToys #2
Posted 13 August 2012 - 12:44 PM
The server had a few problems Here is fixed ver.
Spoiler

term.clear() --Clear any text
term.setCursorPos(1,1) --Set cursor position to beginning
write"This is not a user-accessible computer." -- User information
write"Log in at one of the USER PCs." --User information
local firstCycle = true -- Used to determine whether to run bootUp function. Boolean variable
local validSender = false --Used to determine whether valid information has been entered. Boolean variable
local modemSide = "top" --Determines modem side. String variable
local valid = false --Used to determine whether check is made as to whether to return password or not. Boolean variable
users = {{uname = "XenoG", pword = "pl455ey"},
{uname = "Test", pword = "user"},
{uname = "Xenogene", pword = "lolgasm"}
} --Users table. Left is key, right is value. In effect Username-Password (Key-Value)
senders = {18,20} --Table of all computer ID's that can access server.
function bootUp() --New function "bootUp()"
    rednet.open(modemSide) --Sends message to rednet to make sure the modem is open
end --End function
while true do --Main server loop
    validSender = false --set validSender to false. Needs to be done every loop round
    if firstCycle then --If the this is the first cycle then
	    bootUp() --Call the bootUp() function
    end --End IF statement
    senderId, message, distance = rednet.receive() --rednet.receive() function call. Loop waits here until a message is received
    for i,v in ipairs(senders) do --For index, value, search in the table senders, then execute next line
	    if v == senderId then --If v is equal to a senderId (found in senders table) then
		    validSender = true --Set validSender to true
		    break --Break the loop
	    end --End IF statement
    end --End FOR loop
    if validSender then --If validSender is true, execute next line
	    for k,v in pairs(users) do --For index, value, search in the table users, then execute next line
		    print(message)
		    print(v.uname)
		    print(v.pword)
		    if message == v.uname then --If k is equal to message (found in users table) then
			    valid = true --Set valid to true
			    password = v.pword -- Set password to the value of v
			    break -- this is needed so valid = false isnt called
		    else --Otherwise
			    valid = false --Set valid to false
		    end --End IF statement
	    end --End FOR loop

	    if valid then --If valid is true, execute next line
		    rednet.send(senderId, password) -- Send the senderID and the password to rednet
		    print(senderId.." "..password)
	    else --Otherwise
		    rednet.send(senderId, "Not Valid") --Send a Not Valid message to rednet
		    print(senderId.."".."Not Valid")
	    end --End IF statement
    end --End IF statement
end --End WHILE TRUE DO loop
XenoG #3
Posted 13 August 2012 - 01:58 PM
Thanks for the quick reply! Much appreciated. However, I've had some more problems.

Using the code you edited (good touch with printing login attempts by the way. It actually helped identify my next issue), I changed the server code. However, It's still throwing up a Not Authorised error on the client PC. I've attached a screenshot of the server-side code to show the (possible?) problem.

Screenshot:

http://imgur.com/UwSQQ

EDIT: For clarification of my opinion, it looks as though it is recognising validSender as both true and false (hence showing the Not Valid message) - That said, it doesn't explain why it's posting the senderId or showing all of the available users.

EDIT 2: Just realised the Not Valid option is printed when valid is set to false (bottom of the code). Still, very confused.
Edited on 13 August 2012 - 12:13 PM
BigSHinyToys #4
Posted 14 August 2012 - 05:22 AM
I cant reproduce the error I have made some changes that may help track down its location Please try to reproduce that error and then tell me how you did it. also I have added a new variable called DeBug at the top when set to true it will print useful information when set to false it won't give away your pass.
Spoiler

term.clear() --Clear any text
term.setCursorPos(1,1) --Set cursor position to beginning
write"This is not a user-accessible computer." -- User information
write"Log in at one of the USER PCs." --User information
print("")
local firstCycle = true -- Used to determine whether to run bootUp function. Boolean variable
local validSender = false --Used to determine whether valid information has been entered. Boolean variable
local modemSide = "top" --Determines modem side. String variable
local valid = false --Used to determine whether check is made as to whether to return password or not. Boolean variable
local DeBug = true -- turns off prints so people cant see passwrds
users = {{uname = "XenoG", pword = "pl455ey"},
{uname = "Test", pword = "user"},
{uname = "Xenogene", pword = "lolgasm"}
} --Users table. Left is key, right is value. In effect Username-Password (Key-Value)
senders = {18,20} --Table of all computer ID's that can access server.
function bootUp() --New function "bootUp()"
    rednet.open(modemSide) --Sends message to rednet to make sure the modem is open
end --End function
local function printTEST(...) -- allows you to turn off prints of ueser name set local DeBug = true to local DeBug = false
    if DeBug then
	    print(...)
    end
end
while true do --Main server loop
    validSender = false --set validSender to false. Needs to be done every loop round
    local user = nil
    local password = nil
   
    if firstCycle then --If the this is the first cycle then
	    bootUp() --Call the bootUp() function
    end --End IF statement
    senderId, message, distance = rednet.receive() --rednet.receive() function call. Loop waits here until a message is received
    for i,v in ipairs(senders) do --For index, value, search in the table senders, then execute next line
	    if v == senderId then --If v is equal to a senderId (found in senders table) then
		    validSender = true --Set validSender to true
		    break --Break the loop
	    end --End IF statement
    end --End FOR loop
    if validSender then --If validSender is true, execute next line
	    printTEST("MES "..message)
	    for k,v in pairs(users) do --For index, value, search in the table users, then execute next line
		    printTEST(k..")U "..v.uname.." P "..v.pword)
		    if message == v.uname then --If k is equal to message (found in users table) then
			    valid = true --Set valid to true
			    password = v.pword -- Set password to the value of v
			    user = v.uname
			    break -- this is needed so valid = false isnt called
		    else --Otherwise
			    valid = false --Set valid to false
		    end --End IF statement
	    end --End FOR loop

	    if valid then --If valid is true, execute next line
		    rednet.send(senderId, password) -- Send the senderID and the password to rednet
		    printTEST("Sent "..senderId.." "..password)
		    print("User "..user.." Requested Login")
	    else --Otherwise
		    rednet.send(senderId, "Not Valid") --Send a Not Valid message to rednet
		    printTEST("Sent "..senderId.." ".."Not Valid")
		    print("Atempted Login Failed")
	    end --End IF statement
    end --End IF statement
end --End WHILE TRUE DO loop
XenoG #5
Posted 14 August 2012 - 09:55 AM
Alrighty, I've implemented said changes to code and here is what I'm coming up with:

http://imgur.com/8DHmx
BigSHinyToys #6
Posted 14 August 2012 - 11:27 AM
Alrighty, I've implemented said changes to code and here is what I'm coming up with:

http://imgur.com/8DHmx
If you are typing this in manually then that is the only cause I can think of for it failing. I have set up a server and couple co computers for testing and It is all working fine on them.

Here is my copy of client / server start from my working PC's

Client
Spoiler

local locker = true
local attempted_login = true
local password_server = 19 -- change to the ID of your password server computer
rednet.open("top") -- change to the side your rednet modem is on
while locker do
attempted_login = false
term.clear()
term.setCursorPos(1,1)
print("Welcome to a USERS PC : Roaming Profile Enabled")
print("What would you like to do?")
print("[1] Login (*)")
print("[2] Shutdown")
write("> ")
local input = read()
if input == "2" then
  os.shutdown()
elseif input == "1" then
  attempted_login = true
  print("Please login...")
  write("Username: ")
  local username = read()
  write("Password: ")
  local password = read("*")
  rednet.send(password_server, username)
  senderId, message, distance = rednet.receive(5)
  if password == message then
   locker = false
   term.clear()
   term.setCursorPos(1,1)
   print("Welcome ", username)
  else
   print("Not authorised.")
   sleep(1)
  end
else
  print("Command not recognised...")
  sleep(2)
end
end
Server
Spoiler

term.clear() --Clear any text
term.setCursorPos(1,1) --Set cursor position to beginning
write"This is not a user-accessible computer." -- User information
write"Log in at one of the USER PCs." --User information
print("")
local firstCycle = true -- Used to determine whether to run bootUp function. Boolean variable
local validSender = false --Used to determine whether valid information has been entered. Boolean variable
local modemSide = "top" --Determines modem side. String variable
local valid = false --Used to determine whether check is made as to whether to return password or not. Boolean variable
local DeBug = true -- turns off prints so people cant see passwrds
users = {{uname = "XenoG", pword = "pl455ey"},
{uname = "Test", pword = "user"},
{uname = "Xenogene", pword = "lolgasm"}
} --Users table. Left is key, right is value. In effect Username-Password (Key-Value)
senders = {18,20} --Table of all computer ID's that can access server.
function bootUp() --New function "bootUp()"
rednet.open(modemSide) --Sends message to rednet to make sure the modem is open
end --End function
local function printTEST(...) -- allows you to turn off prints of ueser name set local DeBug = true to local DeBug = false
if DeBug then
  print(...)
end
end
while true do --Main server loop
validSender = false --set validSender to false. Needs to be done every loop round
local user = nil
local password = nil

if firstCycle then --If the this is the first cycle then
  bootUp() --Call the bootUp() function
end --End IF statement
senderId, message, distance = rednet.receive() --rednet.receive() function call. Loop waits here until a message is received
for i,v in ipairs(senders) do --For index, value, search in the table senders, then execute next line
  if v == senderId then --If v is equal to a senderId (found in senders table) then
   validSender = true --Set validSender to true
   break --Break the loop
  end --End IF statement
end --End FOR loop
if validSender then --If validSender is true, execute next line
  printTEST("MES "..message)
  for k,v in pairs(users) do --For index, value, search in the table users, then execute next line
   printTEST(k..")U "..v.uname.." P "..v.pword)
   if message == v.uname then --If k is equal to message (found in users table) then
    valid = true --Set valid to true
    password = v.pword -- Set password to the value of v
    user = v.uname
    break -- this is needed so valid = false isnt called
   else --Otherwise
    valid = false --Set valid to false
   end --End IF statement
  end --End FOR loop

  if valid then --If valid is true, execute next line
   rednet.send(senderId, password) -- Send the senderID and the password to rednet
   printTEST("Sent "..senderId.." "..password)
   print("User "..user.." Requested Login")
  else --Otherwise
   rednet.send(senderId, "Not Valid") --Send a Not Valid message to rednet
   printTEST("Sent "..senderId.." ".."Not Valid")
   print("Atempted Login Failed")
  end --End IF statement
end --End IF statement
end --End WHILE TRUE DO loop

I would serest trying this in a off line creative world copying this code into a file instead of typing it.
Spoiler
XenoG #7
Posted 15 August 2012 - 11:29 AM
Thanks again for the reply. I was indeed typing the code in manually, so I was obviously messing up somewhere. As for the code, it works beautifully, thank you. I ended up using a program that typed the code for you, and so I just pasted the raw code in that way. Again, I'd like to say thanks for all of the help.