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

[LUA]Handshake over a rednet network

Started by mogey, 15 April 2012 - 09:44 PM
mogey #1
Posted 15 April 2012 - 11:44 PM
I want my client software and server software to "shake hands" before continuing and sending messages. How would I allow a computer to be working normally and when it recieves a rednet message to see if its equal to a certain piece of text and then start passing data to it?
Heres my code so far,
Client

rednet.open("right")
rednet.open("left")
rednet.open("bottom")
rednet.open("top")
rednet.open("back")
rednet.open("front")
local ip = os.computerID()
local serverip = "";
local user = "";
function connect()
print("Connecting user: "..user.." to server "..serverip)
rednet.broadcast("ping")
id, message = rednet.receive(5)
if message == "handshake" then
print("Hand shake successful, loading data from server")
else
print("Hand shake failed, please check your server id and try again")
sleep(1)
login()
end
end
function login()
print("Welcome to the Chat Network")
print("Please enter your nick")
nick = read()
print("Please enter the ID of the server you wish to connect to")
serv = read()
user = nick
serverip = serv
connect()
end
login()
Server

rednet.open("right")
rednet.open("left")
rednet.open("bottom")
rednet.open("top")
rednet.open("back")
rednet.open("front")
local ip = os.computerID()
local serverip = "";
local user = "";
function connect()
local ping,p2,p3 = os.pullEvent()
id, message = rednet.receive()
print("Connections enabled")
print(ping)
print(p2)
print(p3)
end
connect()
I also want to be able to use the variable serverip in the rednet.send command, but that does not work as it returns "Rednet expected a positive number"
Hawk777 #2
Posted 16 April 2012 - 09:58 AM
When it says “positive number”, it means not a string. Replace this:
serverip = serv
with this:
serverip = tonumber(serv)
mogey #3
Posted 16 April 2012 - 03:24 PM
When it says “positive number”, it means not a string. Replace this:
serverip = serv
with this:
serverip = tonumber(serv)
Thanks for fixing that, forgot about converting datatyps('doh), do you know how I could run the machine to still function normally and not hang but if it recieves a rednet message labeled ping it will receive it and send a message back?
Hawk777 #4
Posted 19 April 2012 - 07:37 AM
Sure, your server code looks a bit confused. You're doing os.pullEvent() followed immediately by rednet.receive(). If a rednet message arrives while you're inside os.pullEvent(), it will be delivered as an event; you don't need to subsequently rednet.receive() it. The first return value, which you've called "ping", will contain the string "rednet_message". The second, which you've called "p2", will contain the sender ID, and the third, which you've called "p3", will contain the message (the fourth, had you provided it, would contain the distance to the sender in metres). As such, you can handle other events while also waiting for rednet messages.