Before you try any rednet or networking, make sure that you have a modem on the computer and have opened it!
'rednet.receive()' returns two values: The numerical ID of the computer that sent the message and the message itself.
For rednet to receive and send messages you need a modem to be on your computer and opened via code. I suggest you check out
this tutorial on rednet by one of the Administrators of this site.
Also, to have an infinite loop you'll need to use any type of loop really, but the 'while' loop is a favorite of mine.
Here is some simple code that will accomplish what you're asking for:
rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");
while true do
local function clear() term.clear(); term.setCursorPos(1, 1); end
sender, message = rednet.receive();
clear();
print( message );
end
rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");
Quick rundown on the code:
rednet.open("right");
rednet.open("left");
rednet.open("back");
rednet.open("front");
rednet.open("top");
rednet.open("bottom");
These statements would open a modem on the respective side, meaning that if there isn't a modem on the side opened there is no harm done. This allows for flexibility meaning that the user could put their modem on any side they want without having to edit the code.
while true do
This code basically states that while true do whatever we put between this line and the end statement. True will obviously always be true and therefore will run until terminated by the user.
local function clear() term.clear(); term.setCursorPos(1, 1); end
This is a little function for clearing the screen every time a message is received.
sender, message = rednet.receive();
This code here runs the rednet.receive() function with no arguments, meaning there is no timeout and therefore will listen for a message forever, preventing all other program execution.
clear();
This line will call the clear() function that we defined earlier, clearing the screen.
print( message );
This statement will do just that: print the message once received.
end
This statement is required at the 'end' of a loop declaration. It defines the end of all the code that should be executed by the loop. Once the program reaches the 'end' statement it will "loop" back to the beginning of the loop.
rednet.close("right");
rednet.close("left");
rednet.close("back");
rednet.close("front");
rednet.close("top");
And finally, this few statements here close all of the modems open, if there wasn't a modem to begin with there is still no harm done. These 'close' statements are not required but are strongly suggested.
Hope I helped! :)/>/>