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

Multi String over Rednet

Started by dexter9, 24 February 2013 - 04:14 AM
dexter9 #1
Posted 24 February 2013 - 05:14 AM
Hi im trying to make an email program which connects a dns sever which then forwards it on to the receiving client. Please dont ask why! Basically what i need to know is can i send "email adress, message" in the same rednet message. Email address is used as it is easier to remember than numeric IDS. The DNS server resolves these and forwards it on. So can i send 2 strings saved by 2 different "=read()" and then message it through rednet. If not how can i do this keeping the dns part in mind?
Thanks in advance
remiX #2
Posted 24 February 2013 - 05:22 AM
hmm you could use string.match for this… but I'm not sure if this is the correct way to use it …

Sending side:

message = "This is my message that will go with the below email"
email = "email@address.com"

s = message .. "|" .. email

rednet.open("right")
rednet.send( 2, s )

Receiving:

rednet.open("right")
id, msg = rednet.recieve()

message, email = msg:match( "(.-)|(.+)" )

print(message)
print(email)

Output should be
This is my message that will go with the below email
email@address.com
dexter9 #3
Posted 24 February 2013 - 06:46 AM
i get startup:13: attempt to index? (a nil value) as an error?
Victor #4
Posted 24 February 2013 - 07:41 AM
You can send tables over rednet. Put e-mail adress and the message in the same table and send it to the server. Server can easily handle them seperately. I'm too lazy to type some actual code but you can send tables via rednet with the textutils.serialize(), and textutils.unserialize() commands (converting the table to string and converting it back basically). You can look them up in the CC wiki.
dexter9 #5
Posted 24 February 2013 - 07:50 AM
I prefer REMIXS way it appears simpler….
CAn you help with that?
remiX #6
Posted 24 February 2013 - 08:05 AM
i get startup:13: attempt to index? (a nil value) as an error?

And I am eating something. Take a guess what it is?

Please post the code so we can help, just giving us the error doesn't help you or us.

Also, I tried the method I posted and it works for me, Used this:



message = "This is my message that will go with the below email"
email = "email@address.com"

s = message .. "|" .. email

newMessage, newEmail = s:match( "(.-)|(.+)" )

print(newMessage)
print(newEmail)
dexter9 #7
Posted 24 February 2013 - 08:13 AM
Its my DNS server;
http://pastebin.com/GbYi6ewZ
LBPHacker #8
Posted 24 February 2013 - 08:22 AM
I prefer REMIXS way it appears simpler….

Nope, that's the harder way to do it

Send it this way:

local mytable = {}
mytable.address = "email here"
mytable.message = "message here"
rednet.send(target, textutils.serialize(mytable))

And receive it this way:

local sender, message = rednet.receive(timeout)
local mytable = textutils.unserialize(message)
print(mytable.address) -- prints address
print(mytable.message) -- prints message
remiX #9
Posted 24 February 2013 - 08:38 AM
Well do you know how to use rednet? :P/>

s = rednet.receive()
should be
s, msg = rednet.receive()

rednet.receive() returns the ID of the sender, the message and the distance

edit:
Can I asked what this program is receiving via rednet
dexter9 #10
Posted 24 February 2013 - 08:43 AM
The exact thing you posted!
dexter9 #11
Posted 24 February 2013 - 08:50 AM
Ok. As you can see by reading the upload i told the DNS server to print sent when the rednet was forwarded on. It does not display that. The first issue has been resolved but now it wont send i on?
LBPHacker #12
Posted 24 February 2013 - 09:04 AM
Try
e, s, msg = os.pullEvent("rednet_message")
instead of
s = rednet.receive()
and
rednet.send(v, message)
instead of
rednet.send(v, k)
and of course
if tostring(email) == k then
instead of
if tostring(message) == k then

Edit: Sorry, updated.
remiX #13
Posted 24 February 2013 - 09:35 AM
Oh so you want a main computer to forward an incoming 'message/email' to another computer? Or am I wrong?

When I asked what is sending to the program, I mean like what is the program receiving lol.

I'm really not sure how you're making this email client, but take these two programs I just made for example - check attachment for picture

Receiving code / Main Server Computer Code
rednet.open( "right" )

local user, message, id, err
local tIDS = {
	["remiX"] = 2,
	["john"] = 1
}

while true do
	term.clear() term.setCursorPos( 1, 1 )
	print( "This is the server computer, please leave me alone!\n" )
	print( "Latest activity:" )
	print( "      Last User: " .. ( user or "no one yet" ) )
	print( "        Last ID: " .. ( id or "no one yet" ) )
	print( "   Last Message: " .. ( message or "no one yet" ) )
	print( "       Last err: " .. ( err or "none yet" ) )
	local sID, msg = rednet.receive()
	user, message = msg:match( "(.-)|(.+)" )
	for userName, userID in pairs( tIDS ) do
		if user == userName and userID == sID then
			id = userID
			err = user .. " sent to self"
			rednet.send( sID, "self-send" )
			break
		elseif user == userName then
			id = userID
			rednet.send( id, msg )
			break
		end
	end
end

Sending Code / Client PC's
local serverComputer = 0 -- the id of the "SERVER COMPUTER"
rednet.open( "right" )

local function send()
	while true do
		term.clear()
		term.setCursorPos( 1, 1 )
		write( "To which user would you like this message to be forwarded to?\n > " )
		local user = read()

		write( "And what is the message you want to forward?\n > " )
		local message = read()

		local s = user .. "|" .. message
		rednet.send( serverComputer, s )
		
		print("Sent!")
	end
end

local function receive()
	local sID, msg
	repeat sID, msg = rednet.receive() until sID == serverComputer
	term.clear()
	term.setCursorPos( 1, 1 )
	if msg == "self-send" then
		print( "Failed to send message:\nYou can't send to yourself." )
	else
		local user, message = msg:match( "(.-)|(.+)" )
		print( "You have received mail!" )
		print( "     From: " .. user )
		print( "  Message: " .. message )
	end
end

parallel.waitForAny( send, receive )
dexter9 #14
Posted 24 February 2013 - 09:40 AM
Still not working. It receives it and decodes it but it cant send it on?
dexter9 #15
Posted 24 February 2013 - 09:43 AM
Ok trying that now..
remiX #16
Posted 24 February 2013 - 09:50 AM
Had a few mistakes in that code, it would say the message is from yourself…

Fixed version:

Main Server PC:
Spoiler
rednet.open( "right" )

local fromUser, toUser, message, id, err
local tIDS = {
	["remiX"] = 2,
	["john"] = 1
}

local function getFromUser( id )
	for u, i in pairs( tIDS ) do
		if id == i then return u end
	end
	return 'unknown'
end

while true do
	term.clear() term.setCursorPos( 1, 1 )
	print( "This is the server computer, please leave me alone!\n" )
	print( "Latest activity:" )
	print( "      Last User: " .. ( fromUser or 'no one yet' ) .. ' to ' .. ( toUser or "no one yet" ) )
	print( "        Last ID: " .. ( id or "no one yet" ) )
	print( "   Last Message: " .. ( message or "no one yet" ) )
	print( "       Last err: " .. ( err or "none yet" ) )
	local sID, msg = rednet.receive()
	fromUser = getFromUser( sID )
	toUser, message = msg:match( "(.-)|(.+)" )
	for userName, userID in pairs( tIDS ) do
		if toUser == userName and userID == sID then
			id = userID
			err = toUser .. " sent to self"
			rednet.send( sID, "self-send" )
			break
		elseif toUser == userName then
			id = userID
			rednet.send( id, fromUser .. "|" .. message )
			break
		end
	end
end

Client PC
Spoiler
local serverComputer = 0 -- the id of the "SERVER COMPUTER"
rednet.open( "right" )

local function send()
	while true do
		term.clear()
		term.setCursorPos( 1, 1 )
		write( "To which user would you like this message to be forwarded to?\n > " )
		local user = read()

		write( "And what is the message you want to forward?\n > " )
		local message = read()

		local s = user .. "|" .. message
		rednet.send( serverComputer, s )
		
		print("Sent!")
	end
end

local function receive()
	local sID, msg
	repeat sID, msg = rednet.receive() until sID == serverComputer
	term.clear()
	term.setCursorPos( 1, 1 )
	if msg == "self-send" then
		print( "Failed to send message:\nYou can't send to yourself." )
	else
		local user, message = msg:match( "(.-)|(.+)" )
		print( "You have received mail!" )
		print( "     From: " .. user )
		print( "  Message: " .. message )
	end
	sleep(5)
end

while true do
	term.clear() term.setCursorPos( 1, 1 ) print("Type yes to send or receive an email")
	if read() == "yes" then
		parallel.waitForAny( send, receive )
	end
end

New picture below
dexter9 #17
Posted 24 February 2013 - 11:50 PM
Thanks LBPhacker and remiX for all your help it works perfectly! Now i just need to know how to add accounts from the client and saving it to the servers database……
LBPHacker #18
Posted 25 February 2013 - 12:50 AM
Give me ten minutes…
theoriginalbit #19
Posted 25 February 2013 - 12:51 AM
Give me ten minutes…
Just make sure you incorporate this
LBPHacker #20
Posted 25 February 2013 - 01:07 AM
Give me ten minutes…
Just make sure you incorporate this

Sorry, Huh?
dexter9 #21
Posted 25 February 2013 - 01:33 AM
Thanks. Im very greatful for everyones help.
LBPHacker #22
Posted 25 February 2013 - 01:36 AM
Ok, this will take more that ten mins, but I'm almost ready with the client (it contains app. 148 lines of code at the moment)
LBPHacker #23
Posted 25 February 2013 - 02:41 AM
So… These are basic "alpha" releases, nothing serious. Tested, but may contain bugs.

Server (pastebin get CuUrgsec server)

Client (pastebin get amBVeQLX client)