This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Razputin's profile picture

rednet.receive

Started by Razputin, 27 January 2013 - 12:08 PM
Razputin #1
Posted 27 January 2013 - 01:08 PM
What code do I need for a computer to receive a message from another computer? I've tried several things but they all return errors.
GravityScore #2
Posted 27 January 2013 - 02:44 PM
Computer 1 (the sender):

-- Open the rednet modem. This assumes you have the modem on the top of the computer. You can change it to left, right, bottom, etc...
rednet.open("top")

-- Broadcast a message to all computers
print("Sending Message")
rednet.broadcast("Hello, World!")

-- Wait for a response from another computer
print("Waiting for response...")
id, message = rednet.receive()

print("We received a response from comptuer ID: " .. id)
print("With the message: " .. message)

Computer 2 (The responder):

-- Open the modem. Same as the first program
rednet.open("top")

-- Wait for a message
print("Waiting for a message")
id, message = rednet.receive()

print("Message received from comptuer ID: " .. id)
print("Message says: " .. message)

-- Send a response to the computer that just sent us a message
print("Sending response")
rednet.send(id, "Hello there! Received your message!")

If you place a new computer, copy the second script to it, and run that script, it will start waiting for rednet messages to be sent to it. Then place computer 1 and run the first program. You should see that it broadcasts a message, then receives a response from the second computer. The second computer will show that it received a message, and then sent a response.