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

rednet.lookup returning nil

Started by Chickenbreadlp, 03 June 2016 - 09:03 PM
Chickenbreadlp #1
Posted 03 June 2016 - 11:03 PM
I'll just begin at the beginning:
I'm working on something like a FTP for CC and when I came to testing, I ran into a bug, even though on one computer there was a rednet host the other computer returned nil when running redent.lookup. But the strange thing about this bug is, that on the first try rednet.lookup returns the id of the other computer. I can only fix rednet.lookup by rebooting the computer which is the rednet host…

Can somebody help me?
Original code
KingofGamesYami #2
Posted 03 June 2016 - 11:38 PM
rednet.host shouldn't be called in a loop. However, short of calling rednet.unhost, it should work.

Edit: Noticed checkModem is unnecisarily lengthy, so I re-wrote it:


local function checkModem()
  if rednet.isOpen() then
    return true
  end
  for _, side in ipairs( rs.getSides() ) do
    if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
     rednet.open( side )
     return true
    end
  end
  return false
end
Edited on 03 June 2016 - 09:45 PM
Bomb Bloke #3
Posted 04 June 2016 - 01:39 AM
Other than the "constantly calling rednet.host()" thing (which is redundant, but shouldn't cause problems), the only bit of related weirdness I'm seeing is when a "change hostname" command comes through. You never update the value of "runningHost".
Chickenbreadlp #4
Posted 04 June 2016 - 07:47 AM
Other than the "constantly calling rednet.host()" thing (which is redundant, but shouldn't cause problems), the only bit of related weirdness I'm seeing is when a "change hostname" command comes through. You never update the value of "runningHost".
I didn't tested the runServerShell yet. I was so confused about the rednet.lookup problem, that I didn't tested anything else.

rednet.host shouldn't be called in a loop. However, short of calling rednet.unhost, it should work.

Edit: Noticed checkModem is unnecisarily lengthy, so I re-wrote it:


local function checkModem()
  if rednet.isOpen() then
	return true
  end
  for _, side in ipairs( rs.getSides() ) do
	if peripheral.isPresent( side ) and peripheral.getType( side ) == "modem" then
	 rednet.open( side )
	 return true
	end
  end
  return false
end
Well, thank you. I'm still learning lua, in case you asked yourself, why it's so lengthy.

EDIT
I tried everything I could imagine, what the problem could be caused by, and nothing worked. I just don't understand this, because if I run the code line by line in the lua program, it works. :huh:/>
Edited on 04 June 2016 - 10:49 AM
Bomb Bloke #5
Posted 04 June 2016 - 01:25 PM
Ah, I think I see the problem.

Rednet works by sending specially formatted messages through modems, which destination systems receive as "modem_message" events in their event queue (same as if you transmit directly through modems, without using rednet at all). When a given system boots, a special function called rednet.run() is started in parallel with the shell. One of its jobs is to look for modem_message events, and if it finds they were transmitted via rednet.send(), it queues corresponding rednet_message events (which you can either pull directly, or via rednet.receive()). It's also rednet.run() which responds to lookup requests.

ComputerCraft 1.6 added a "repeat" script to each system, which waits for incoming rednet messages and re-sends them through all available modems. The purpose of this is to make it easier to extend the range of networks - these days, with a single computer running that script through an Ender Modem, you've got pretty much the whole Minecraft server covered.

The introduction of this script created a bit of a problem in that it became possible for multiple modem_message events to be received for each message sent via rednet. To counter this, Dan added an extra bit of data to each modem_message associated with rednet - a random number. If two modem_messages with the same random number are received within half a minute, rednet.run() will not create a corresponding rednet_message, assuming them to be duplicate copies.

The odds of erroneous collisions are pretty low under normal circumstances, but your "unfoldkey" functions complicate matters by messing with the random number seed. More specifically, they keep resetting it back to a particular static value, which makes the odds of rednet collisions "near certain"!

Change these lines:

math.randomseed(math.random())

… so that the seed is a bit more unpredictable:

math.randomseed(os.clock())

math.random() isn't suitable as a seed in this instance because you've just set a seed of "keyhash" (which is the same every time - you read it from a file) and then rolled 32 values. The 33rd value, the one you use to set the new seed, is always going to be the same. The system clock is nearly certain to be different every time you check it, however.

The thing I'm scratching my head over is that if this is the problem - and I can't really see any other alternatives - it'd be the seed of the client system at fault, not the seed of the server. Hrm. Do systems share seeds? Might have to look into that one… Maybe you're simply running similar code on the client system…?
Chickenbreadlp #6
Posted 04 June 2016 - 06:09 PM
Ah, I think I see the problem.

Rednet works by sending specially formatted messages through modems, which destination systems receive as "modem_message" events in their event queue (same as if you transmit directly through modems, without using rednet at all). When a given system boots, a special function called rednet.run() is started in parallel with the shell. One of its jobs is to look for modem_message events, and if it finds they were transmitted via rednet.send(), it queues corresponding rednet_message events (which you can either pull directly, or via rednet.receive()). It's also rednet.run() which responds to lookup requests.

ComputerCraft 1.6 added a "repeat" script to each system, which waits for incoming rednet messages and re-sends them through all available modems. The purpose of this is to make it easier to extend the range of networks - these days, with a single computer running that script through an Ender Modem, you've got pretty much the whole Minecraft server covered.

The introduction of this script created a bit of a problem in that it became possible for multiple modem_message events to be received for each message sent via rednet. To counter this, Dan added an extra bit of data to each modem_message associated with rednet - a random number. If two modem_messages with the same random number are received within half a minute, rednet.run() will not create a corresponding rednet_message, assuming them to be duplicate copies.

The odds of erroneous collisions are pretty low under normal circumstances, but your "unfoldkey" functions complicate matters by messing with the random number seed. More specifically, they keep resetting it back to a particular static value, which makes the odds of rednet collisions "near certain"!

Change these lines:

math.randomseed(math.random())

… so that the seed is a bit more unpredictable:

math.randomseed(os.clock())

math.random() isn't suitable as a seed in this instance because you've just set a seed of "keyhash" (which is the same every time - you read it from a file) and then rolled 32 values. The 33rd value, the one you use to set the new seed, is always going to be the same. The system clock is nearly certain to be different every time you check it, however.

The thing I'm scratching my head over is that if this is the problem - and I can't really see any other alternatives - it'd be the seed of the client system at fault, not the seed of the server. Hrm. Do systems share seeds? Might have to look into that one… Maybe you're simply running similar code on the client system…?
Thank you! It finally works! But I didn't know, that math.randomseed(math.random()) doesn't give you completly random numbers. Also I didn't know, that rednet sends a random number with the message and ignores that message, when it gets two message with the same random number. Thank you anyway. I can finally finish it with user functions 'n stuff.