Rednet API

From ComputerCraft
Jump to: navigation, search

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.

FunctionFunctionrednet.broadcast
Broadcasts a string message over the predefined rednet.CHANNEL_BROADCAST channel (default 65535), assuming rednet has been opened. The message will be received by every device listening to rednet.

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.
Syntax rednet.broadcast()
Returns nil
Part of CC:Tweaked (source)
API rednet
ExampleExampleBroadcast a 'Ping' message over rednet.
Broadcasts 'Ping' over the defined rednet.CHANNEL_BROADCAST channel.
Code
rednet.broadcast('Ping')
Output Users running rednet.receive will now have received 'Ping' in their programs.


FunctionFunctionrednet.close
Closes rednet on the modem attached to side.

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.
Syntax rednet.close()
Returns nil
Part of CC:Tweaked (source)
API rednet
See also rednet.open
ExampleExampleClose the top modem.
If rednet is open on the top modem on the computer, this will close it.
Code
rednet.close("top")
Output Rednet events will no longer trigger on the top modem.

Rednet.host Rednet.isOpen Rednet.lookup

FunctionFunctionrednet.open
Opens rednet on the modem attached to side.

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.
Syntax rednet.open()
Returns nil
Part of CC:Tweaked (source)
API rednet
See also rednet.close
ExampleExampleOpen the top modem.
Opens rednet on the top modem of the computer.
Code
rednet.open("top")
Output Rednet events will now trigger on the top modem.
ExampleExampleOpen all connected modems
This is a little trick using peripheral.find. It accepts a function as the second argument, which is called for each matching peripheral. By using rednet.open here, every connected modem is opened.
Code
peripheral.find("modem", rednet.open)


FunctionFunctionrednet.receive
Waits for a Rednet message to be received by an opened modem, or until timeout seconds elapse. Takes another optional parameter, which limits messages able to be received to those broadcasted or sent with a matching protocol.

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.
Syntax rednet.receive()
Returns number senderID, any message, string protocol
Part of Rednet API (source)
API rednet
ExampleExampleReceive any message with no timeout
Since we have no arguments, rednet.receive will wait for the next message from any protocol. If a rednet message was received with the text "Hello world!", the the output would be as follows:
Code
local id, message, protocol = rednet.receive()
print(message)
Output
"Hello world!"
ExampleExampleReceive any message with a timeout
Since we have our timeout specified, rednet.receive will wait for the next message from any protocol, or until the timeout is up. If a rednet message was received with the text "Now there's a timeout!" before the timeout is up, the the output would be as follows:
Code
local id, message, protocol = rednet.receive(30)
print(message)
Output
"Now there's a timeout!"
ExampleExampleReceive messages of a certain protocol with a timeout
Since we have our timeout and protocol specified, rednet.receive will wait for the next message from a specific protocol, or until the timeout is up. If a rednet message was received with the text "Now there's a protocol!" and protocol "ComputerCraft" before the timeout is up, the the output would be as follows:
Code
local id, message, protocol = rednet.receive(30, "ComputerCraft")
print(message)
Output
"Now there's a protocol!"

Rednet.run

FunctionFunctionrednet.send
Sends a Rednet message to be received by a specific computer (whose id is id.) Takes another optional parameter, protocol, which specifies what the message is for. It returns false if the message was unable to be sent, but true if it was sent.

Make sure your modem is opened with rednet.open!

Note Note: This function call requires a Modem peripheral to be attached to the computer. See Networking for more information.

Note Note: Rednet is not secure against interception or spoofing. See Network security#Rednet for more information.
Syntax rednet.send()
Returns bool success
Part of Rednet API (source)
API rednet
ExampleExampleSend a message to a computer
We send a message to the computer whose id is 567. If the message could be sent, we should see an output of true:
Code
print(rednet.send(567, "Hello world!"))
Output
true
ExampleExampleSend a message to a computer with a protocol
We now send a message to the computer whose id is 890, with a protocol of "ComputerCraft". If it was sent, we will see a true output:
Code
print(rednet.send(890, "Hello world!", "ComputerCraft"))
Output
true

Rednet.unhost