Networking & rednet

Rednet ping

Two computers with wireless modems: one sends pings over rednet, the other answers.

Likes
0
Views
2
In-game installs
0

Uses

  • modem

Capabilities

  • Uses rednet

Licence

MIT

startup.lua 903 bytes Raw
1-- Both computers run this program; their labels (set in the scene) decide
2-- what each one does.
3
4peripheral.find("modem", rednet.open)
5local me = os.getComputerLabel()
6print(("I'm %s, computer #%d"):format(me, os.getComputerID()))
7
8if me == "ponger" then
9 -- Let others find this computer by name, then answer every ping.
10 rednet.host("ping", "ponger")
11 while true do
12 local from, message = rednet.receive("ping")
13 print(("#%d says %q"):format(from, message))
14 rednet.send(from, "pong" .. message:sub(5), "ping")
15 end
16else
17 -- Find the other computer, then ping it a few times.
18 local other
19 repeat
20 other = rednet.lookup("ping", "ponger")
21 until other
22 for i = 1, 5 do
23 rednet.send(other, "ping " .. i, "ping")
24 local from, reply = rednet.receive("ping", 2)
25 print(from and ("#%d answered %q"):format(from, reply) or "No answer")
26 sleep(1)
27 end
28 print("Done.")
29end

Install in your world

wget run https://ccbin.dev/i/IsZvHUC8
wget https://ccbin.dev/raw/IsZvHUC8 startup.lua
Embed on your site
<iframe src="https://ccbin.dev/IsZvHUC8/embed" width="800" height="520" style="border:0" loading="lazy" allow="autoplay" title="Rednet ping on ccbin.dev"></iframe>

Sites that understand oEmbed, such as Discord and many forums, make the embed from the program's link.

Two computers with wireless modems: one sends pings over rednet, the other answers.

This is one of the examples in the Workbench. Run it here, or open it in the Workbench to change it.

Comments

No comments yet.

Sign in to comment.