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

rednet.send question

Started by jtdavis99, 04 March 2012 - 10:02 PM
jtdavis99 #1
Posted 04 March 2012 - 11:02 PM
I want to make some sort of send and receive email type program. I have asked for an input and labeled it as a (the message) and then asked for another input and labeled it as b (the computer id you're sending it to) How would I go about mixing it into rednet.send? Would rednet.send( b, a ) work?
MysticT #2
Posted 04 March 2012 - 11:15 PM
You have to convert the id to number:

write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
    rednet.send(id, msg)
else
    print("ID must be a number.")
end
jtdavis99 #3
Posted 04 March 2012 - 11:30 PM
You have to convert the id to number:

write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
	rednet.send(id, msg)
else
	print("ID must be a number.")
end

Is there a way I can save the id they put in as, say, sid and send it to a specific predetermined ID? I want to have it so the message runs through a server and is checked for words and things before it is sent.
MysticT #4
Posted 05 March 2012 - 12:16 AM
Yes, you should do something like:

local serverID = 1 -- Change this to the server ID
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
       rednet.send(serverID, id..": "..msg)
else
       print("ID must be a number.")
end
And then the server does wathever you want, and sends the msg to the destination.
Example:

local evt, id, msg = os.pullEvent("rednet_message")
local sId, sMsg = string.match(msg, "(%d+): (.+)")
local destID = tonumber(sId)
-- do something with the msg
rednet.send(destID, sMsg)
jtdavis99 #5
Posted 05 March 2012 - 12:28 AM

local evt, id, msg = os.pullEvent("rednet_message")
local sId, sMsg = string.match(msg, "(%d+): (.+)")
local destID = tonumber(sId)
-- do something with the msg
rednet.send(destID, sMsg)

Where would I put this and what would I modify? It looks like someone ripped up a line of binary on the second line :unsure:/>/>

I'm assuming I would put it in the server comp's startup, and have an input called destID?
MysticT #6
Posted 05 March 2012 - 12:35 AM
If you send the messages like I told you, the server should receive something like: (ID): (message)
An example message:
4: Hello
then, it gets the id and the message from that (that's what the second line does), does whatever you want and sends the message.
To use it you should have to put that in some loop inside the server program.
jtdavis99 #7
Posted 05 March 2012 - 12:45 AM
So, would this be the overall code?

local serverID = 33 -- Change this to the server ID
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
	   rednet.send(serverID, id..": "..msg)
else
	   print("ID must be a number.")
end
local evt, id, msg = os.pullEvent("rednet_message")
local sId, sMsg = string.match(msg, "(%d+): (.+)")
local destID = tonumber(sId)
-- do something with the msg
rednet.send(destID, sMsg)

Or this

Inside computer
local serverID = 33 -- Change this to the server ID
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
	   rednet.send(serverID, id..": "..msg)
else
	   print("ID must be a number.")
end

Inside Server Terminal Startup

while true do
local evt, id, msg = os.pullEvent("rednet_message")
local sId, sMsg = string.match(msg, "(%d+): (.+)")
local destID = tonumber(sId)
-- do something with the msg
rednet.send(destID, sMsg)
MysticT #8
Posted 05 March 2012 - 01:08 AM
They have to be in separate computers (the server and the clients).
In the server, put this as startup (replacing the parts needed):

while true do
    local evt, id, msg = os.pullEvent("rednet_message")
    local sId, sMsg = string.match(msg, "(%d+): (.+)")
    if sId and sMsg then
        local destID = tonumber(sId)
	    -- do what you need with the message
	    rednet.send(destID, sMsg)
    end
end
And the client should have the send code (the same as before).
jtdavis99 #9
Posted 05 March 2012 - 01:17 AM
(replacing the parts needed):

while true do
	local evt, id, msg = os.pullEvent("rednet_message")
	local sId, sMsg = string.match(msg, "(%d+): (.+)")
	if sId and sMsg then
		local destID = tonumber(sId)
		-- do what you need with the message
		rednet.send(destID, sMsg)
	end
end

What parts should be replaced?
MysticT #10
Posted 05 March 2012 - 01:30 AM
Where it says: – do what you need with the message
you have to do what you want with the message (check words, etc.)
And you should change serverID to your server's id.
jtdavis99 #11
Posted 05 March 2012 - 01:46 AM
I have put the top part in the send computer's startup, and the bottom in the server's startup. I have another computer waiting for an input. The server's id is 33. The send's id is 32. The receive's id is 31. For some reason, the server will wait for a message, but won't receive one or pass it on to the receiving computer. This is the sending comp's startup:
local serverID = 33 -- Change this to the server ID
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
	   rednet.send(serverID, id..": "..msg)
else
	   print("ID must be a number.")
end

This is the server's startup:
while true do
		local evt, id, msg = os.pullEvent("rednet_message")
		local sId, sMsg = string.match(msg, "(%d+): (.+)")
		if sId and sMsg then
				local destID = tonumber(sId)
				-- do what you need with the message
				rednet.send(destID, sMsg)
		end
end

I noticed that the server's code doesn't have a rednet.open command or a receiving command. Doesn't it need to receive the message to pass it on? The server just has a blank CraftOS 1.3 screen and won't receive or even pass on the message :unsure:/>/>
jtdavis99 #12
Posted 05 March 2012 - 02:13 AM
And, by the way, thank you SO much for helping with this. It was a massive confusion for me and, you're awesome :unsure:/>/>
MysticT #13
Posted 05 March 2012 - 05:03 PM
I forgot the open call :unsure:/>/>, here's the fixed code:
Sender:

local serverID = 27 -- Change this to the server ID
local sSide = "top" -- Change the side

rednet.open(sSide)
write("Enter the message: ")
local msg = read()
write("Enter the id: ")
local id = tonumber(read())
if id ~= nil then
	rednet.send(serverID, id..": "..msg)
	print("Message sent.")
else
	print("ID must be a number.")
end
Server:

local sSide = "top" -- Change the side

rednet.open(sSide)
while true do
	local evt, id, msg = os.pullEvent("rednet_message")
	print("Message from ", id, ": ", msg)
	local sId, sMsg = string.match(msg, "(%d+): (.+)")
	if sId and sMsg then
		local destID = tonumber(sId)
		-- do what you need with the message
		print("Sending message to ", destID)
		rednet.send(destID, sMsg)
	end
end

I tested it and works B)/>/>
If you are using modems you can use a function to autodetect where the modem is (so you don't have to change the side in code). This is how I do it:

function OpenModem()
	for _, s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false, "No modem found"
end
jtdavis99 #14
Posted 05 March 2012 - 08:17 PM
Thank you SO much. you are incredibly helpful and are very knowledgeable :unsure:/>/>