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

[lua][question] How does rednet.send() work?

Started by bbqroast, 23 March 2012 - 06:35 PM
bbqroast #1
Posted 23 March 2012 - 07:35 PM
Hey I have the following code. When I client connects it checks it isn't already connected and then adds it and sends the "ready" signal back. The issue is that while the code seems to be working fine nothing is sent to the client! The clients code is fine (I used the "LUA" command to open the lua console on another machine and manually sen tback my reply to the client, it recieved it fine).

clients = {}
client_user = {}
user = {}
function addClient(id)
write("Adding Client #")
write(id)
write("\n")
size = table.getn(clients)
i = 1
while i <= size do
  if clients[i] == id then
   write("Refused Client ")
   write(id)
   write(", already joined \n")
   return false
  end
  i = i + 1
end
ssiReply = "ready"
print("Sending response")
rednet.send(id, ssiReply)
n = size + 1
clients[n] = id
write("Client ")
write(id)
write(" successfully joined! \n")
end
function addUser(user, pass)
end
function authUser(user, pass)
end
function init(side)
write("Micro-Roast Domain server 1.0 Starting! \n")
write("MRD is starting on the ")
write(side)
write(" side! \n")
rednet.open(side)
user[0] = {"root","pwd"}
end

init("back")
run = true
while run == true do
local sender, message = rednet.receive()
write("Computer #")
write(sender)
write(" said ")
write(message)
write("\n")
if message == "join" then
  addClient(sender)
end
if message == "shutdown" then
  print("Shutting down")
  run = false
end
end
PatriotBob #2
Posted 23 March 2012 - 08:26 PM
Well… without seeing an example the terminal out put to kinda debug what's going on and with out seeing the client's code to make sure that after sending the join request that it's properly awaiting a response. It's really hard to say where it's going awry. But for the most part, after a cursory glance, I don't see any glaring holes in it's logic.

Like you saying that the Terminal says "Client 248 successfully joined!" but that client never receives a response. Then there could either be a problem in the client code, or as simple as rednet.send() will time out on a send operation if the client doesn't respond to the black signal with a red signal.

When rednet communicates it does a little handshake, so if client doesn't respond to the intial handshake, because for instance it's not listening on the port yet, then the rednet.send will timeout and fail silently.

So unless I missed a logic hole in your code, it's really hard to tell what's going wrong.
Luanub #3
Posted 23 March 2012 - 09:55 PM
I don't really see anything wrong with this code. I would consider changing your vars to local in case the globals are causing conflicts somewhere.

You could also try to change to use an event instead of rednet.receive()


while true do
local evnt, sender, message = os.pullEvent("rednet_receive")
write("Computer #")
write(sender)
write(" said ")
write(message)
write("n")
if message == "join" then
  addClient(sender)
end
if message == "shutdown" then
  print("Shutting down")
  break
end
end

I also changed it to a plain while true loop (shouldnt make any difference just my personal preference)
bbqroast #4
Posted 24 March 2012 - 10:25 PM
Thanks. The server is outputting all stages of the connection to the console, and if I spoof the client with a packet sent from another computer (via the "lua" command) it works.

Are "global" (ie non local) variables shared between all computers or just all the functions?

For some reason my computers won't close when i use CTRL+T/E/X so I set up the loop to close on the shutdown command.
EDIT: Never mind, I see what you did.
UPDATE:
I changed back to using rednet.receive() because the other one didn't seem to be working. And then suddenly it all worked perfectly.
Thanks for the help!
UPDATE:
My bad, the client was accepting timed out errors as valid responses.
Server

clients = {}
client_user = {}
user = {}
serverName = "MRD_test"
-- Split function courtesy of the LUA users forum
function split(pString, pPattern)
   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
	  if s ~= 1 or cap ~= "" then
	 table.insert(Table,cap)
	  end
	  last_end = e+1
	  s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
	  cap = pString:sub(last_end)
	  table.insert(Table, cap)
   end
   return Table
end
function addClient(id)
write("Adding Client #")
write(id)
write("n")
size = table.getn(clients)
i = 1
while i <= size do
  if clients[i] == id then
   write("Refused Client ")
   write(id)
   write(", already joined n")
   return false
  end
  i = i + 1
end
write("Sending response: "..serverName.."n")
rednet.send(id, serverName)
n = size + 1
clients[n] = id
write("Client ")
write(id)
write(" successfully joined! n")
end
function addUser(user, pass)
end
function authUser(user, pass)
end
function init(side)
write("Micro-Roast Domain server 1.0 Starting! n")
write("MRD is starting on the ")
write(side)
write(" side! n")
rednet.open(side)
user[0] = {"root","pwd"}
end

init("back")
run = true
while run == true do
local sender, message = rednet.receive()
write("Computer #")
write(sender)
write(" said ")
write(message)
write("n")
messTable = split(message, ";")
if messTable[1] == "join" then
  addClient(sender)
end
if messTable[1] == "shutdown" then
  print("Shutting down")
  run = false
end
end
Client (I know it has the login code the server isn't ready for, but it makes no difference).

serverID = nil
loggedIn = false
serverName = ""
message = "error"
function init(side)
rednet.open(side)
rednet.broadcast("join;")
serverID, message = rednet.receive(4)
if message ~= nil then
  print("Server found, ID ")
  print(serverID)
  print("n")
  serverName = message
  return true
else
 
end
end
function login()
while true do
   print("Username: ")
   loginUser = io.read()
   print(loginUser)
   print("@")
   print(serverName)
   print("'s Password: ")
   loginPass = io.read()
   rednet.send(serverID,"user:"..loginUser..";pass:"..loginPass)
  
  end
end
if init("back") == true then
login()
end
bbqroast #5
Posted 26 March 2012 - 03:06 AM
Bump.
Does anyone have working rednet.send() and rednet.receive() code? I can get it to work in the lua emulation console but not in actual scripts (rednet.broadcast() and rednet.send() works fine thou!).