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

rednet.lookup Return Hostnames

Started by Rectar2, 28 August 2014 - 09:20 PM
Rectar2 #1
Posted 28 August 2014 - 11:20 PM
As useful as rednet.lookup can be, it can still be a bit useless in a few situations. For example, on a server I was playing on, I was using rednet.lookup to find a chat server, and I got results. However, since it only returns the IDs, I couldn't actually connect to it using the Chat program, since it uses the Hostname. So my idea is that rednet.lookup, rather than return a table of IDs, could return a table of tables containing the id and hostname. An example of what it could look like:

{
    {id=28,name="creeper"},
    {id=12,name="chat"},
}
Lyqyd #2
Posted 29 August 2014 - 12:59 AM
You can use the dns protocol to ask those IDs what their hostnames are.
Rectar2 #3
Posted 29 August 2014 - 03:46 AM
You can use the dns protocol to ask those IDs what their hostnames are.
How would we do that? Me and three other people have been trying to figure this out for the past half-hour.
Lyqyd #4
Posted 29 August 2014 - 06:25 AM
This is a naive implementation of my above suggestion, which would look up hostnames for all computers hosting on the "chat" protocol.


local hosts = rednet.lookup("chat")
local hostnames = {}
for k, v in pairs(hosts) do
  rednet.send(v, {sType = "lookup", sProtocol = "chat"}, "dns")
  local message
  repeat
	message = {rednet.receive("dns")}
  until type(message) == "table" and message[1] == v and type(message[2]) == "table" and message[2].sType == "lookup response"
  hostnames[k] = message[2].sHostname
end

On the second-to-last line, you could replace the k with v to have the table be indexed by the computer IDs instead of the index the computer ID was at in the first table.