Posted 20 September 2014 - 06:17 PM
Hi,
I made an email program for a server that, when it receives a message saying "read", it will read a file on the server, and will send that back as a string.
However, this does not work as when the server is meant to loop back to the beginning, it does not. It then returns "attempt to index nil" when I try to query the server again from the client.
Also, now the server and client don't work at all?
What has happened?
The Code:
Server:
The Client:
I made an email program for a server that, when it receives a message saying "read", it will read a file on the server, and will send that back as a string.
However, this does not work as when the server is meant to loop back to the beginning, it does not. It then returns "attempt to index nil" when I try to query the server again from the client.
Also, now the server and client don't work at all?
What has happened?
The Code:
Server:
--#[Email Server]--
local st = 1
sleep(0.1)
while st == 1 do
rednet.open("right")
print("STARTED")
local event, id, text = os.pullEvent() --#Checks for compose
if event == "rednet_message" then
if text == "EmailCompose" then
print("CEmail R")
local st = 2
local st = 2
while st == 2 do
local event,id, text = os.pullEvent() --#Checks for UID
if event == "rednet_message" then
print(text..": UID")
local UserID = text
local st = 3
while st == 3 do
print("DEBUG: 3")
local event, id, text = os.pullEvent() --#Checks for SID
if event == "rednet_message" then
print("DEBUG: 4")
local SendID = text
local st = 4
while st == 4 do
print("DEBUG: 5")
local event, id, text = os.pullEvent() --#Checks for message
if event == "rednet_message" then
print("DEBUG: 6")
local message = text
local st = 5
while st == 5 do
local event, id, text = os.pullEvent() --#Checks for end command
if event == "rednet_message" then
if text == "finito" then
sleep(0.1)
rednet.send(id, "true")
local h = fs.open(SendID..".em", "a")
h.writeLine("User ID "..UserID.." sent a message")
h.writeLine("Message: "..message)
shell.run("startup")
else
print("FAILED AT 5.")
local st = 1
end
end
end
end
end
end
end
end
end
elseif text == "EmailRead" then
print("ERead")
local st = 100
rednet.send(id, "true")
while st == 100 do
local event, id, text = os.pullEvent()
if event == "rednet_message" then
local UserID = text
local h = fs.open(UserID..".em", "r")
emailCache = h.readAll()
sleep(0.1)
rednet.send(id, emailCache)
local st = 1
end
end
end
end
end
The Client:
--#[Email Client]--
term.clear()
term.setCursorPos(1,1)
print("User Referrer ID:")
local userid = read()
print("Command: ")
local s = 1
while s == 1 do
local command = read()
if command == "compose" then
print("Composing new email.")
rednet.open("right")
rednet.send(2, "EmailCompose")
sleep(0.1)
rednet.send(2, userid)
sleep(0.1)
print("User to send to:")
local sendid = read()
rednet.send(2, sendid)
print("Message:")
local messageSend = read()
rednet.send(2, messageSend)
sleep(0.1)
rednet.send(2, "finito")
local s = 2
while s == 2 do
local event, id, text = os.pullEvent()
if event == "rednet_message" then
if text == "true" then
print("Success!")
sleep(2)
local s = 1
else
print("Success!")
sleep(2)
local s = 1
end
end
end
elseif command == "read" then
print("Querying server.")
rednet.send(2, "EmailRead")
local s = 100
while s == 100 do
local event, id, text = os.pullEvent()
if event == "rednet_message" then
if text == "true" then
rednet.send(id, userid)
local s = 101
while s == 101 do
local event, id, text = os.pullEvent()
if event == "rednet_message" then
print(text)
sleep(5)
local s = 1
end
end
else
print("Error at end")
sleep(2)
local s = 1
end
end
end
end
end