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

Rednet gone wrong..

Started by xXAdamXx, 22 May 2014 - 11:23 AM
xXAdamXx #1
Posted 22 May 2014 - 01:23 PM
Hi!
Adam again,
and now that ii know a bit of lua, i tried to make somthing that has never became open source (I think..)
A password server.
now, the client can send but hangs/Goes back to the beginning every time I try to recive the packet.

Here are the two GUI's

Client

while true do
rednet.open("bottom")
print("Password")
input = read()
rednet.send(6,input)
local senderId, message, distance = rednet.receive()

if senderId == "6" then
if message == "true" then
rs.setOutput("right", true)
sleep(3)
rs.setOutput("right", false)
end
if message == "false" then
print("wrong!")

end
end
end
And the server Gui


rednet.open("top")
while true do
local senderId, messsage, distance = rednet.receive()
if message == "12345" then
print("true")
rednet.send(7,"true")
else
rednet.send(senderId,"false")
end
senderId, message = nil
senderId, message = rednet.receive()
end

Please try them out, and tell me my flaw!
thanks,
Adam hendi.
Edited on 22 May 2014 - 11:24 AM
RoD #2
Posted 22 May 2014 - 02:02 PM
get rid of the last 2 lines in the server loop (these):

senderId, message = nil
senderId, message = rednet.receive()
macss_ #3
Posted 22 May 2014 - 03:08 PM
Another thing, if you are using the latest stable version of CC (1.63 I think) you could use proctocols instead of computer id's doing the changing of computers a lot easier…


rednet.broadcast("Hello!", "pass_in")

Will only be received by computers that are waiting in the "pass_in" protocol..


_, message, __ = rednet.receive("pass_in")

This will make your programimg life a lot easier, I think
Dog #4
Posted 22 May 2014 - 06:27 PM
Another way to make use of the new protocol feature of rednet in cc1.6+.

Server

rednet.host([protocol name], [host name])

Client

local server = rednet.lookup([protocol name], [host name])
Edited on 22 May 2014 - 05:03 PM
xXAdamXx #5
Posted 22 May 2014 - 09:08 PM
Another thing, if you are using the latest stable version of CC (1.63 I think) you could use proctocols instead of computer id's doing the changing of computers a lot easier…


rednet.broadcast("Hello!", "pass_in")

Will only be received by computers that are waiting in the "pass_in" protocol..


_, message, __ = rednet.receive("pass_in")

This will make your programming life a lot easier, I think
Well, I Cant really "Announce" the Password, or any Hacker that has open ports can recive it aswell, also, all the doors will open.
Another way to make use of the new protocol feature of rednet in cc1.6+.

Server

rednet.host([protocol name], [host name])

Client

local server = rednet.lookup([protocol name], [host name])
I have that, though I would like the Whole thing to sort out like this:

Correct
Operator enters Pass
Client ==> Server: is 12345 the right Pass?
Server ==> Client: Yes
Client ==> Door: Open up.

Wrong
Operator enters pass
client ==> Server: is 12345 the right Pass?
server ==> client: No.
client ==> Operator: Denied.
Edited on 22 May 2014 - 07:11 PM
Dog #6
Posted 22 May 2014 - 09:49 PM
Please post your code as it is now (don't update the OP, please post in reply - I'd like to compare what you have now to what you started with).
xXAdamXx #7
Posted 22 May 2014 - 11:20 PM
Please post your code as it is now (don't update the OP, please post in reply - I'd like to compare what you have now to what you started with).

--client code
while true do
rednet.open("bottom")
print("Password")
input = read()
rednet.send(6,input)
local senderId, message, distance = rednet.receive()

if senderId == "6" then
if message == "true" then
rs.setOutput("right", true)
sleep(3)
rs.setOutput("right", false)
end
if message == "false" then
print("wrong!")

end
end
end

--server side

rednet.open("top")
while true do
local senderId, messsage, distance = rednet.receive()
if message == "12345" then
print("true")
rednet.send(7,"true")
else
rednet.send(senderId,"false")
end

end
Dog #8
Posted 23 May 2014 - 02:39 AM
I've indented your code so it's easier to read and troubleshoot. Here's what I found…

In your client code you're looking for a string on line nine - you should be looking for a number - remove the quotes around 6

In your server code I noticed a spelling error (check all your variables named 'message'); and I noticed that you change between using senderID and 7 to talk to your client - this will work, but you should pick one or the other for stylistic reasons and to make troubleshooting a bit easier.

Make those fixes and let me know how the code works for you.


--client code
while true do
  rednet.open("bottom")
  print("Password")
  input = read()
  rednet.send(6,input)
  local senderId, message, distance = rednet.receive()

  if senderId == "6" then
    if message == "true" then
      rs.setOutput("right", true)
      sleep(3)
      rs.setOutput("right", false)
    end
    if message == "false" then
      print("wrong!")
    end
  end
end

--server side
rednet.open("top")
while true do
  local senderId, messsage, distance = rednet.receive()
  if message == "12345" then
    print("true")
    rednet.send(7,"true")
  else
    rednet.send(senderId,"false")
  end
end
Edited on 23 May 2014 - 03:56 AM
xXAdamXx #9
Posted 17 July 2014 - 04:58 PM
for some reason, it dose not connect. I may just need to change the address, but it may be a communication
issue.

never mind. Lua is case sensitive with varibles?
I never knew that.
Now, Im going hard core. im adding more machines, and more passwords.