Minecraft Blogs / Tutorial

ComputerCraft: RedNet

  • 1,808 views, 3 today
  • 1
  • 0
finley243's Avatar finley243
Level 54 : Grandmaster Geek
74
Hello guys,

Today I have a ComputerCraft tutorial for you. This one is on a server/client web system built with RedNet.

First, you need a computer to use a client, which needs a modem. In real life, this could be the computer you are using to read this tutorial. On this computer, we need to create a web browser to view content sent by the server. To do this, I've created this program:
------------------------------------------------------
rednet.open("left")
input = read()

rednet.broadcast(input)
id,msg,dis = rednet.receive()
term.clear()
term.setCursorPos(1,1)
print (msg)
------------------------------------------------------
This program first enables the wireless modem, which is on the left in this case. It then takes a user text input. After the user inputs the "URL" of the server they wish to connect to, the program broadcasts the "URL" to all computers on the network. The program then waits until it recieves a message. It then clears the terminal and displays the content of the server.


Next, you need a computer to use as a server. Note, the server program will run continuously until either the Minecraft world is closed or the program is terminated using Ctrl+T. This means that you need a computer to use as a dedicated server. The code for the server is as follows:
------------------------------------------------------
while true do
rednet.open("left")
id,msg,dis = rednet.receive()

if msg == "http://server.net" then
rednet.send(id, "Yay, you connected to the server!")
end
end
------------------------------------------------------
This program is essentially the reverse of the web browser program. The "while true do" is to make the program loop forever. It enables the modem and then waits for a message. When you send a message on the web browser program, it broadcasts the message to all computers, including this server. When this server recieves a message, it checks to see if the "URL" sent in the message is equall to it's own "URL". If it is not, it does nothing and continues checking for incoming messages. If the "URLs" do match, the server sends a message containing the content which the web browser will display. To avoid problems while using multiple web browsers at once, like on a multiplayer server, I have added code so that the server will only send the content to the computer that sent the request. This is done by by using "rednet.send" to send a message to a specific ID, which "rednet.broadcast" cannot do.

This concludes this tutorial. I may create more ComputerCraft tutorials in the future, so be on the lookout. Thanks for reading!
Tags

Create an account or sign in to comment.

Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome