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

Question: Ping brings strange results

Started by LordDrako, 26 May 2012 - 10:22 AM
LordDrako #1
Posted 26 May 2012 - 12:22 PM
Hello,

I am using Computercraft and RedPower and I just build a little network of ca. 12 PCs.
All of these are connected through bundled cables but two (the PC in my house :)/>/> and one of the PCs in the center)
also have a modem.
Now I got a program on two PCs running which just loops with os.pullEvent and responds to network pings (rednet.send(…, "ping"))
with a "pong".
On my home PC I wrote a little ping program which either sends a broadcast ping (when it is launched without paramaters) or it sends a
ping to every hostid it gets as a parameter…

So… now comes the strange stuff :/

When I send a broadcast ping only the PC with modem responds…
When I ping the PC with the modem directly (through the parameter) I get a response from host 14 (which doesn't event exist.. the hostid pinged was 6).
When I ping the PC that doesn't have a modem directly the same happens… I get a response from host 14 (the real hostid is 7)
When I ping a hostid that doesn't exist there is no response at all.

Therefore it seems as if the programs on the pinged hosts receive the pings and also respond to it correctly.
But somehow the ping program receives the wrong host ID…
Also it seems as if rednet.broadcast only works on wireless…

Any help would be appreciated…
If you need my programs I can attach them here, but currently I think it's not the code.
Lolgast #2
Posted 26 May 2012 - 02:30 PM
If you put the code here, we can see whether it's in the code or not.
LordDrako #3
Posted 26 May 2012 - 03:51 PM
okay, so here is my Ping program:

-- grab command line
local args = { ... }
local failure = false

-- setup
rednet.open("left")
rednet.open("back")

-- no parameters -> broadcast ping
if #args == 0 then
  print("Sending broadcast ping.")
  rednet.broadcast("ping")
else
  for n = 1, #args do
	local hostid = tonumber(args[n])
	if hostid == nil then
	  print("Error: " .. args[n] .. " is not a valid host id.")
	  failure = true
	  break
	end
	print("Sending ping to " .. tostring(hostid))
	rednet.send(hostid, "ping")
  end
end

-- wait for responses
while not failure do
  local evt, param1, param2 = os.pullEvent()
  if (evt == "rednet_message") and (param2 == "pong") then
	print("Received response from " .. tostring(param1))
  end
  if (evt == "char") and (param1 == "q") then
	print("Exiting...")
	break
  end
end

-- shutdown
rednet.close("left")

This program runs on the server without modem (only redpower)

-- This host just prints all inputs on the monitor
-- setup monitor
local screen = peripheral.wrap("back")
term.redirect(screen)
print("Output redirected to monitor.")

-- setup network
rednet.open("right")
print("Network opened.")
local hostid = os.getComputerID()
print("Host ID: " .. tostring(hostid))

-- setup redstone
local input = redstone.getBundledInput("left")
print("Initialized redstone input.")
print("Current redstone value: " .. tostring(input))

print("Running main loop.")
-- main loop
while true do
  local evt, p1, p2, p3, p4, p5 = os.pullEvent()
  -- rednet messages
  if evt == "rednet_message" then
	print("Received network message from " .. tostring(p1) .. ": " .. p2)
	-- handle pings
	if p2 == "ping" then
	  print("Sending response to " .. tostring(p1) .. ": pong")
	  sleep(1)
	  rednet.send(p1, "pong")
	end
  -- redstone input from the left cable
  elseif evt == "redstone" then
	local incoming = redstone.getBundledInput("left")
	if not (incoming == input) then
		print("Redstone signal changed to: " ..  tostring(incoming))
		input = incoming
	end
  end
end
This PC has two redpower inputs:
One for the cable network and one which interfaces with a Redpower PC through an IO expander.


Okay, here is the PC with Modem and RedPower:

-- setup
print("Setting up networking...")
rednet.open("right")
rednet.open("back")

-- handle rednet input
function onRednetReceive(sender, message)
  -- handle pings
  if message == "ping" then
	print("Received ping from " .. tostring(sender))
	sleep(1)
	rednet.send(sender, "pong")
  end
end

-- main loop
print("Running main loop.")
while true do
  local evt, p1, p2, p3, p4, p5 = os.pullEvent()
  print("Incoming event: " .. evt)
  if evt == "rednet_message" then
	onRednetReceive(p1, p2)
  end
end
here one port is the modem (back) and the other (left) is the cable.
This one is the PC that responds to broadcast pings.

As I said both respond to direct pings but somehow the ping program receives an host id of 14 for both PCs :/
LordDrako #4
Posted 26 May 2012 - 06:13 PM
so, I am still experimenting and there are even more strange things :/

so lets declare some things…
These are the 3 hosts:

2 - my home host with ping program (cable and modem)
6 - host with modem and cable
7 - host with cable and monitor

at first I only saw this:
when doing a broadcast ping from my home I just received a response from 6.
but now I realized, that 7 somehow receives a ping from 6 instead of 2…
so 7 responds to 6 with a pong which 6 doesn't seem to receive as it produces no output :/

I'd guess something in the lowlevel rednet protocol is messed up here :S
The 14 I receive on my home pc when pinging the targets directly might be a result of 7 and 2 being punched together in a rednet packet (sender and receiver)
thoug I cannot logically explain this as the bitpattern of 7 already contains the 2 bit… and why would anyone just multiply sender and receiver …
bbqroast #5
Posted 27 May 2012 - 03:58 AM
I tried cabled networking, it was fail.
Bossman201 #6
Posted 29 May 2012 - 06:36 PM
LordDrako it looks like you have a problem in the cables, not knowing how to use them in code, which was probably bbqroast's problem as well. As far as I could tell from quickly reading your post over the code is solid though, so you just need to figure out how to use bundled cables. I don't know much about them so I can't help with that but I suggest just writing a new topic and asking how to use bundled cables in the code.