rednet.receive

From ComputerCraft
Jump to: navigation, search

rednet.receive
Function
Syntax
rednet.receive()
Returns number senderID, any message, string protocol
API rednet
Source Rednet API (source)

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.

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!"