The goals of this was a proof of concept type of thing:
- send a message from a client computer to an idle server
- make server save the message, and rebroadcast the contents of the file back to the client
Client:
local function Startup()
term.clear()
term.setCursorPos (1,1)
print (" this Computer's id: ", os.getComputerID())
print (" Set receiving computer's id:")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
print ()
print (" message:")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
term.setCursorPos(5,4)
repeat
local compid = tonumber(read())
until compid
term.setCursorPos(5,4)
print ("id saved")
term.setCursorPos(5,9)
local alv3 = read()
term.setCursorPos(5,9)
print ("message saved")
sleep(0.5)
Sender()
sleep(2)
end
function Sender()
term.clear()
term.setCursorPos (1,1)
print ("sending message in: ")
print ("3")
sleep(1)
print ("2")
sleep(1)
print ("1")
rednet.open("right")
sleep(1)
rednet.send(tonumber(compid), alv3)
sleep(1)
rednet.close("right")
print ("message sent")
sleep(1)
Startup()
end
function Receive()
term.clear()
term.setCursorPos (1,1)
print (" this Computer's id: ", os.getComputerID())
print (" waiting for message:")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
print ()
print (" Terminal:")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
while true do
rednet.open("right")
senderID, message = rednet.receive()
term.setCursorPos (5,4)
print (message)
sleep(0.5)
term.setCursorPos (5,9)
print (senderID)
local event = os.pullEvent("key")
Startup()
break
end
end
Startup()
Server Code:
local function Startup()
term.clear()
term.setCursorPos (1,1)
print (" this Computer's id: ", os.getComputerID())
print (" Set this Computer's label:")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
print ()
print (" Set The Computer Terminal's ID")
print (" +--------------------------+ ")
print (" | ")
print (" +--------------------------+ ")
term.setCursorPos(5,4)
local IgnLabel = read()
os.setComputerLabel(IgnLabel)
term.setCursorPos(5,4)
print ("Label successful")
term.setCursorPos(5,9)
local compid = tonumber(read())
term.setCursorPos(5,9)
print ("ID saved")
sleep(2)
print ("storing message")
WriteFile()
sleep(1)
print ("sending stored messages")
SendFile()
print ("message sent!")
sleep(5)
Startup()
end
function WriteFile()
rednet.open("right")
repeat
local id, msg = rednet.receive()
until id == compid
rednet.close()
if fs.exists("messages") == false then
fs.makeDir("messages")
local msgs = fs.open("messages", "a")
msgs.write(msg)
msgs.close()
elseif fs.exists("messages") == true then
local msgs = fs.open("messages", "a")
msgs.write(msg)
msgs.close()
else
term.clear()
term.setCursorPos(1,1)
print ("failed to write file, or receive message")
sleep(3)
Startup()
end
end
function SendFile()
local SendIt = fs.open("messages", "r")
local shipment = SendIt.readAll()
SendIt.close()
rednet.open("right")
rednet.send(compid, shipment)
rednet.close("right")
end
Startup()
There might be some unforseen bugs on both programs. I haven't been able to test it any farther than the client failing to send the message. Any help and tips are appreciated, also if you want to elaborate or go into depth on tips&tricks when coding that would make my code better/easier please keep it ELI5. I'm not a good programmer :P/>