When you receive a rednet message, you will need to check the senderID to check if it's a legit ID from the client/server.'
From there, the message must be in a way where you can split it into the client/id/message, like so:
Server codeSpoiler
-- Variables
local turtleIDs = { ['3'] = true, ['6'] = true } -- table with all the turtle IDs that are in use for the program
function split(str, divider)
local parts = {}
for v in str:gmatch('[^'..divider..']+') do
table.insert(parts, v)
end
return parts
end
local function receiveRednet()
local sID, msg, temp
repeat
sID, msg = rednet.receive()
temp = split( msg, '_' )
-- what a client pc could send in a rednet message
-- can be something like this for easy use:
-- 'client_turtleid_message'
-- temp[1] will be client
-- temp[2] will be the id of the turtle
-- temp[3] will be the message
until turtleIDs[temp[2]] -- so it will keep doing it until a message is received that has a legit turtle ID
-- okay, so legit turtle id detected, lets forward the message
-- to the turtle
-- but first return the variables
return { client = temp[1], turtleid = temp[2], msg = temp[3] } -- returns a table with the correct details
end
while true do -- infinite loop
term.clear()
term.setCursorPos(1,1)
print('Waiting for a rednet response...')
local info = receiveRednet()
-- If it got here, then there is a legit turtle id
-- now let's send the message to the turtle
local s = info[1] .. '_' .. info[3] -- string to send, separated with a _ for splitting, format: 'client_message'
rednet.send(tonumber(info[2]), s) -- sends the string to the turtle
end
Client codeSpoiler
-- Variables
local turtleIDs = { ['3'] = true, ['6'] = true } -- table with all the turtle IDs that are in use for the program
local serverID = 1 -- the id of the server
-- you could just have the server to just respond success or failure,
-- but that means more rednet is involved ...
local function sWrite( str, x, y )
term.setCursorPos( x, y )
write( str )
end
local function getValue( x, y )
term.setCursorPos( x, y )
return read()
end
while true do
term.clear()
sWrite( ' Client: ', 1, 1 )
sWrite( 'TurtleID: ', 1, 2 )
sWrite( ' Message: ', 1, 3 )
local client = getValue( 11, 1 ) .. '[' .. os.getComputerID() .. ']' -- name with the computer's ID in brackets at the end
local turtleID = getValue( 11, 2 )
local message = getValue( 11, 3 )
if turtleIDs[turtleID] then -- check if it is a valid turtle id
print( '\nSuccess!')
local s = client .. '_' .. turtleID .. '_' .. message
rednet.send( serverID, s )
else
print( '\nInvalid Turtle ID!')
end
sleep(1.3)
end
Untested but I can't see any obvious mistakes :P/>…
Added as many comments as I could.