Communicate with other computers by using modems. rednet provides a layer of abstraction on top of the main modem peripheral, making it slightly easier to use.
In order to send a message between two computers, each computer must have a modem on one of its sides (or in the case of pocket computers and turtles, the modem must be equipped as an upgrade). The two computers should then call rednet.open , which sets up the modems ready to send and receive messages.
Once rednet is opened, you can send messages using rednet.send and receive them using rednet.receive . It's also possible to send a message to every rednet-using computer using rednet.broadcast .
While rednet provides a friendly way to send messages to specific computers, it doesn't provide any guarantees about security. Other computers could be listening in to your messages, or even pretending to send messages from other computers!
If you're playing on a multi-player server (or at least one where you don't trust other players), it's worth encrypting or signing your rednet messages.
Several rednet messages accept "protocol"s - simple string names describing what a message is about. When sending messages using rednet.send and rednet.broadcast , you can optionally specify a protocol for the message. This same protocol can then be given to rednet.receive , to ignore all messages not using this protocol.
It's also possible to look-up computers based on protocols, providing a basic system for service discovery and DNS. A computer can advertise that it supports a particular protocol with rednet.host , also providing a friendly "hostname". Other computers may then find all computers which support this protocol using rednet.lookup .
CHANNEL_BROADCAST = 65535 | The channel used by the Rednet API to broadcast messages. |
---|---|
CHANNEL_REPEAT = 65533 | The channel used by the Rednet API to repeat messages. |
MAX_ID_CHANNELS = 65500 | The number of channels rednet reserves for computer IDs. |
open(modem) | Opens a modem with the given peripheral name, allowing it to send and receive messages over rednet. |
close([modem]) | Close a modem with the given peripheral name, meaning it can no longer send and receive rednet messages. |
isOpen([modem]) | Determine if rednet is currently open. |
send(recipient, message [, protocol]) | Allows a computer or turtle with an attached modem to send a message intended for a computer with a specific ID. |
broadcast(message [, protocol]) | Broadcasts a string message over the predefined CHANNEL_BROADCAST channel. |
receive([protocol_filter [, timeout]]) | Wait for a rednet message to be received, or until nTimeout seconds have elapsed. |
host(protocol, hostname) | Register the system as "hosting" the desired protocol under the specified name. |
unhost(protocol) | Stop hosting a specific protocol, meaning it will no longer respond to rednet.lookup requests. |
lookup(protocol [, hostname]) | Search the local rednet network for systems hosting the desired protocol and returns any computer IDs that respond as "r. |
run() | Listen for modem messages and converts them into rednet messages, which may then be received. |
The channel used by the Rednet API to broadcast messages.
The channel used by the Rednet API to repeat messages.
The number of channels rednet reserves for computer IDs. Computers with IDs greater or equal to this limit wrap around to 0.
Opens a modem with the given peripheral name, allowing it to send and receive messages over rednet.
This will open the modem on two channels: one which has the same ID as the computer, and another on the broadcast channel.
rednet.open("back")
peripheral.find("modem", rednet.open)
Close a modem with the given peripheral name, meaning it can no longer send and receive rednet messages.
Determine if rednet is currently open.
Allows a computer or turtle with an attached modem to send a message intended for a computer with a specific ID. At least one such modem must first be opened before sending is possible.
Assuming the target was in range and also had a correctly opened modem, the target computer may then use rednet.receive to collect the message.
rednet.send(2, "Hello from rednet!")
Broadcasts a string message over the predefined CHANNEL_BROADCAST channel. The message will be received by every device listening to rednet.
rednet.broadcast("Hello, world!")
Wait for a rednet message to be received, or until nTimeout seconds have elapsed.
local id, message = rednet.receive() print(("Computer %d sent message %s"):format(id, message))
local id, message = rednet.receive(nil, 5) if not id then printError("No message received") else print(("Computer %d sent message %s"):format(id, message)) end
local id, message repeat id, message = rednet.receive() until id == 2 print(message)
Register the system as "hosting" the desired protocol under the specified name. If a rednet lookup is performed for that protocol (and maybe name) on the same network, the registered system will automatically respond via a background process, hence providing the system performing the lookup with its ID number.
Multiple computers may not register themselves on the same network as having the same names against the same protocols, and the title localhost is specifically reserved. They may, however, share names as long as their hosted protocols are different, or if they only join a given network after "registering" themselves before doing so (eg while offline or part of a different network).
Stop hosting a specific protocol, meaning it will no longer respond to rednet.lookup requests.
Search the local rednet network for systems hosting the desired protocol and returns any computer IDs that respond as "registered" against it.
If a hostname is specified, only one ID will be returned (assuming an exact match is found).
local computers = rednet.lookup("chat")> print(#computers .. " computers available to chat") for _, computer in pairs(computers) do print("Computer #" .. computer) end
local id = rednet.lookup("chat", "my_host") if id then print("Found my_host at computer #" .. id) else printError("Cannot find my_host") end
Listen for modem messages and converts them into rednet messages, which may then be received.
This is automatically started in the background on computer startup, and should not be called manually.
Last updated on 2024-08-30