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

Rednet:87: Expexted number issue

Started by Kristian2210, 29 March 2018 - 12:47 PM
Kristian2210 #1
Posted 29 March 2018 - 02:47 PM
I am writing a sort of messaging program, and i have written a little script that can get login information from other computers, and write them down in a file library, although in my "readData" function, i get the issue that rednet expected a number. I initially thought that the issue was there because i was feeding the senders ID into the function as a string, and not an integer, so i used tonumber() to convert it to a number. The weirdest part is that the exact same input in the "createLib" and the rednet functions seem to work perfectly there. It is probably really obvious, but i cant seem to figure out what causes this issue.

The code is unfinished, so many parts may seem a bit weird.


This is the server that writes down and reads the login information:

rednet.open("top")
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Messenger Server Console v0.1")
term.setCursorPos(2,2)
if rednet.isOpen("top") then
  term.setBackgroundColor(colors.green)
  term.clearLine()
  print("Rednet is Online")
else
  term.setCursorPos(2,2)
  term.setBackgroundColor(colors.red)
  term.clearLine()
  print("Rednet is Offline")
end
term.setBackgroundColor(colors.black)
createLib = function(sender, userid, userpw)
  local file = fs.open("users/"..userid,"a")
  file.writeLine(userpw)
  file.close()
  print("Successfully added user "..userpw.." to table!")
  print(sender..tostring(sender))
  rednet.send(sender, "userReg")
end
readData = function(sender, userid, userpw)
  local file = fs.open("users/"..userid,"r")
  local fileData = {}
  repeat
  table.insert(fileData, line)
  local line = file.readLine()
  until line == nil
  file.close()
  local storedInfo = fileData[1]
  if storedInfo == userpw then
	term.setTextColor( colors.green )
	print("Successfully compaired login info")
	rednet.send(sender, "ok")
  else
	term.setTextColor( colors.red )  
	print("Stored info did not match received info")
	rednet.send(sender, "not ok")
  end
end
while true do
  Id, msg = rednet.receive()
  msgTable = textutils.unserialise(msg)

  clientID = tonumber(Id)
  msg1 = msgTable[1]
  msg2 = msgTable[2]
  print(msg1, msg2)

  print("User: "..msg1.." with id "..clientID.." has requested login for userid "..msg2)
  if fs.exists("users/"..msg2) then
	print("Id: "..msg2.." was found in libary")
	print("Matching login info with database...")
	readData(clentID, msg2, msg1)
	print("Data has been read, awaiting status")
  else
	print("Id: "..msg2.." not found in libary, creating path...")  
	createLib(clientID, msg2, msg1)
  end
end



This is the client, very unfinished!

rednet.open("back")
serverId = 18
computerId = os.getComputerID()
_Ui = paintutils.loadImage("chat")
_login = paintutils.loadImage("login")
drawUI = function(number, x, y)
  term.clear()
  if number == 1 then
term.setBackgroundColor(colors.cyan)
	term.clear()
	paintutils.drawImage(_Ui, x, y)  
  elseif number == 2 then
term.clear()
	paintutils.drawImage(_login, x, y)
  end
end

clean = function(x, y)
  term.clear()
  term.setCursorPos(x, y)
end
chatMessage = function(name, msg)
end

chat = function(name)
  drawUI(1, 1, 1)
  term.setCursorPos(11, 1)
  print(" Messenger v0.2")
  while true do
	event, button, x, y = os.pullEvent("mouse_click")
	if button == 1 and x > 10 and y == 18 then
	  term.setCursorPos(11, 18)
	  message = read()	
	end
  end
end
login = function()
  clean(1, 1)
  term.setBackgroundColor(colors.cyan)
  clean(1, 1)
  drawUI(2, 1, 1)
  term.setCursorPos(9,10)
  term.write("Username")
  term.setCursorPos(9,13)
  term.write("Password")
  term.setCursorPos(8, 20)
  term.setBackgroundColor(colors.orange)
  term.write("Delete user")
  term.setCursorPos(9,11)
  term.setBackgroundColor(colors.blue)
  userN = read()
  term.setCursorPos(9,14)
  passw = read("*")
  table = {userN..passw, os.getComputerID()}
  message = textutils.serialize(table)

  rednet.send(serverId, message)
  id, msg = rednet.receive()
  if msg == "userReg" then
	rednet.send(15, userN)
	term.setBackgroundColor(colors.cyan)
	term.setCursorPos(1, 20)
	print("User registerd, please log in again")
	sleep(2)
	login()
  elseif msg == "ok" then
	chat(userN)
	rednet.broadcast(userN)
  elseif msg == "not ok" then
	term.setCursorPos(9,14)
	term.setBackgroundColor(colors.red)
	print(" Wrong! ")
	term.setCursorPos(9, 11)
	print(" Wrong! ")
	sleep(2)
	login()
  end
end
--RUN
login()
Bomb Bloke #2
Posted 29 March 2018 - 03:36 PM
clientId doesn't need to be tonumber()'d, as it already is a number.

You've got a "clentId" hidden in there which is leading to the error.