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

[Lua][Error]

Started by TCGM, 06 July 2012 - 10:18 PM
TCGM #1
Posted 07 July 2012 - 12:18 AM
Hello

While working on my email client, and after some great help in my other topic, I managed to get registration working perfectly. However, I then wrote a program the client can run to request the name and ID of an entry in the table by sending the server a number, gotten from user input. The server keeps giving me an error I can't solve. The error is:

rednet:350:string expected

Server Code:


print("Running eMail server")
table1={}
n=0
rednet.open("right")
while true do
if message == "reglist" then
regname = rednet.receive()
print("Receiving Database Query request from " .. id)
print(table1[regname])
rednet.broadcast(table1[regname])
print("Computer has been sent requested listing.")
end
end


Client Code:




rednet.open("right")
rednet.broadcast("reglist")
print("Querying Server...")
print("Enter Database Position Number:")
rednet.broadcast(read())
print("Sending request to the Server...")
id, message = rednet.receive()
print("Registration Server Response: " .. message)

MysticT #2
Posted 07 July 2012 - 12:22 AM
Well, it would help if we can see the part of the code that stores values in the table, or at least know what's stored in it.
The problem is that either the table doesn't have a value for the given key, or the value is not a string.
TCGM #3
Posted 07 July 2012 - 12:24 AM
Well, it would help if we can see the part of the code that stores values in the table, or at least know what's stored in it.
The problem is that either the table doesn't have a value for the given key, or the value is not a string.
Server:

if message == "register" then
id1, regname = rednet.receive()
rednet.broadcast("reged")
table.insert(table1,regname)
table1[regname]=id
print("Computer " .. table1[regname] .. " registered as name " .. table1[n+1])
print(table.maxn(table1))
n = n+1
end

Client:

rednet.open("right")
rednet.broadcast("register")
print("Registering ID with the server...")
print("Registering Name with the server...")
if os.getComputerLabel() == nil then
name = "Unknown"
else
name = os.getComputerLabel()
end
rednet.broadcast(name)
id, message = rednet.receive()
print("Registration Server Response: " .. message)
if message == "reged" then
print("Successfully Registered with the server as ID " .. os.getComputerID() .. " with name " .. name)
else
print ("Registration Unsuccessful")
end
MysticT #4
Posted 07 July 2012 - 01:09 AM
See, the error is in that part of the code:

id1, regname = rednet.receive()
rednet.broadcast("reged")
table.insert(table1,regname)
table1[regname]=id
You define id1, and then use id.
TCGM #5
Posted 07 July 2012 - 01:36 AM
See, the error is in that part of the code:

id1, regname = rednet.receive()
rednet.broadcast("reged")
table.insert(table1,regname)
table1[regname]=id
You define id1, and then use id.

I'm not sure how, but this server code appears to automatically define "id" as the incoming rednet computer's ID.

EDIT: Being stupid, I am. but what I need to send to the client is not only the value of the variable, but the name as well.

Double EDIT: Upon further inspection, ID is actually being defined due to the fact that rednet.receive() returns (id, message).
MysticT #6
Posted 07 July 2012 - 01:51 AM
rednet.receive() returns the id and the message, but it doesn't define the variables. You need to define them yourself, like:

local id, message = rednet.receive()
And you can define them as whatever you want, like:

local n, j = rednet.receive()
n will contain the id, and j will contain the message.

To send more than one value, you can format the string in some way that you can later decode, or you can store them in a table, serialize it and send it.
Example:
Sender:

local t = {}
t.id = id
t.name = name
rednet.send(someID, textutils.serialize(t))
Receiver:

local id, msg = rednet.receive()
local t = textutils.unserialize(msg)
print(t.id, ": ", t.msg)
TCGM #7
Posted 07 July 2012 - 02:15 AM
Ok, I've fixed that. I also created a Nil Response Error, which checks to see if the entry in the table is empty; if it is, broadcasts the return message as "Nil Error". It's doing this every single time, unless, in the mailserv file, I set the reg variable to a number. Then it returns to the client the correct value of the table entry. Is this an error with the way I have the client sending which slot to check? Because that information does not appear to be getting back to the server.

New Server:


if message == "register" then
id, regname = rednet.receive()
rednet.broadcast("reged")
table.insert(table1,regname)
table1[regname]=id
print("Computer " .. table1[regname] .. " registered as name " .. table1[n+1])
print(table.maxn(table1))
n = n+1
end
if message == "reglist" then
id, message = rednet.receive()
reg = message
print("Receiving Database Query request from " .. id)
if table1[reg] == nil then
i = "Nil Error"
else
i = table1[reg]
end
print(i)
rednet.broadcast(i)
print("Computer has been sent requested listing.")
end

New Client:


rednet.open("right")
rednet.broadcast("reglist")
print("Querying Server...")
print("Enter Database Position Number:")
input = read()
rednet.broadcast(input)
print("Sending request to the Server...")
id, message = rednet.receive()
print("Registration Server Response: " .. message)
MysticT #8
Posted 07 July 2012 - 03:05 AM
If you want to use the id to look in the table, remember that you receive a string, not a number, so you need to convert it to number with tonumber.
TCGM #9
Posted 07 July 2012 - 10:22 AM
I knew it would be something simple like that, a command I didn't know. Thank you! tonumber() solved it. Also, your fs code is extremely helpful as it is the next step of what I'm doing (saving the table to a file).

I love this forum :P/>/>