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

Issues with internet system

Started by Windows10User, 09 May 2018 - 05:33 PM
Windows10User #1
Posted 09 May 2018 - 07:33 PM
I decided to make an internet system for my OS and CraftOS. I made the browser (so far uses the DNS to resolve a domain to an ID) and the DNS. The DNS nicely receives the request for the ID, but doesn't send it, even though both the domain and ID are present in its ids table.

(BTW, I know that the Rednet API has a small DNS inside of it. Don't mention it. Please.)

Browser (or is it so far?):

rednet.open("top")

print("DNS ID:")
local dns = tonumber(read())
print("URL:")
local url = read()

rednet.send(dns, url, "REQUEST_ID")

local event, id, msg, protocol = os.pullEvent("rednet_message")
if protocol == "ID" then
	print(url.." resolved to "..msg)
end

DNS:

local tArgs = {...}

if #tArgs < 1 then
	error("dns <wireless modem side>", 0)
end

local mSide = tArgs[1]
local modem = nil

if peripheral.getType(mSide) ~= "modem" then
	error("No modem detected on the "..mSide.." side of the computer...", 0)
else
	modem = peripheral.wrap(mSide)
	if not modem.isWireless() then
		error("The modem on the "..mSide.." side of the computer is wired!", 0)
	end
end

local ids = {
["cabbage.com"] = 3
}

rednet.open(mSide)

while true do
	local event, id, msg, protocol = os.pullEvent("rednet_message")
	if protocol == "REQUEST_ID" then
		print("Computer #"..id.." requested ID of "..msg)
		for i=1, #ids do
			if ids[msg] then
				rednet.send(id, ids[msg], "ID")
				print("Replied with "..ids[msg].." to computer #"..id)
			end
		end
	end
end
SquidDev #2
Posted 09 May 2018 - 07:48 PM
At a guess, the problematic line is for i=1, #ids do. The length of a table only includes the array part, so in this particular case #ids is 0. Consequently, the loop is body never executed and so nothing is sent. You can just delete that loop, as the counter isn't actually used.
Windows10User #3
Posted 09 May 2018 - 07:53 PM
At a guess, the problematic line is for i=1, #ids do. The length of a table only includes the array part, so in this particular case #ids is 0. Consequently, the loop is body never executed and so nothing is sent. You can just delete that loop, as the counter isn't actually used.

So what am I supposed to do?

EDIT: Look, look, removing the for fixes it! Now I just need to try with multiple domains.

EDIT 2: It works!!! Thanks 4 saving my life, Squid.
Edited on 09 May 2018 - 05:57 PM
Dave-ee Jones #4
Posted 10 May 2018 - 01:23 AM
I would suggest using the modem API and doing your own protocols and DNS via tables. Rednet has a few performance issues and isn't very secure (though, you don't really need the security at this stage). You just have a bit more control.

You can do simple things like this:

local m_TOP = peripheral.wrap("top")
m_TOP.open(os.getComputerID())
m_TOP.transmit(65535,65533,{
  PROTOCOL = "BROADCAST",
  DATA = {
    "jack",
    "jill"
  },
  MY_ID = os.getComputerID()
})
Marc1miner #5
Posted 10 May 2018 - 07:42 AM
I would suggest using the modem API and doing your own protocols and DNS via tables. Rednet has a few performance issues and isn't very secure (though, you don't really need the security at this stage). You just have a bit more control.

You can do simple things like this:

local m_TOP = peripheral.wrap("top")
m_TOP.open(os.getComputerID())
m_TOP.transmit(65535,65533,{
  PROTOCOL = "BROADCAST",
  DATA = {
	"jack",
	"jill"
  },
  MY_ID = os.getComputerID()
})

If you do decide to follow this instruction and write your own protocol, you need to write a receiving modem on the same transmit id. Otherwise you'll be sending but not receiving.
Windows10User #6
Posted 10 May 2018 - 01:30 PM
I think the modem API is too difficult to learn and use in general.
Edited on 10 May 2018 - 11:31 AM