Rednet ping
Two computers with wireless modems: one sends pings over rednet, the other answers.
| 1 | -- Both computers run this program; their labels (set in the scene) decide |
| 2 | -- what each one does. |
| 3 | |
| 4 | peripheral.find("modem", rednet.open) |
| 5 | local me = os.getComputerLabel() |
| 6 | print(("I'm %s, computer #%d"):format(me, os.getComputerID())) |
| 7 | |
| 8 | if 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 |
| 16 | else |
| 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.") |
| 29 | end |
Press Run to boot a computer.
- Modems and rednet work between the computers in the scene. A computer on its own has a wireless modem but no one to talk to.
Network
Every request the program makes, and how the sandbox answered it. The program's own mocks answer first. Pastebin, GitHub and ccbin downloads go through ccbin. Anything else gets a placeholder, or an error if the scene says so. Nothing else leaves your browser unless you turn on live network.
Install in your world
wget run https://ccbin.dev/i/IsZvHUC8wget https://ccbin.dev/raw/IsZvHUC8 startup.luaTwo 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.