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

Rednet help

Started by souper123, 19 October 2014 - 01:26 AM
souper123 #1
Posted 19 October 2014 - 03:26 AM
I am trying to write a program with two computers, a client and a server to send a message to each other. The client sends a message "ping" and the server responds with "pong". when i print the message that the server receives, it outputs something like table: 785a38cb. I have tried using print(message[0]) but that just prints a blank message, not the expected "pong" message.
server code:


local modemSide = "top"
rednet.open(modemSide)
rednet.host("bank", "bankServer")
id, message, distance, protocol = rednet.receive()
print(message)
print(id)
if message == "ping" then
  print("received ping from ID: " .. id)
  rednet.send(id, "pong")
end

Client code:


local modemSide = "top"
local bprotocol = "bankServer"
rednet.open(modemSide)
serverID = rednet.lookup("bank")
rednet.send(5, "ping")
print("sending ping to server ID: " ..serverID)
print("waiting for server ping...")
id, message, distance, protocol = rednet.receive(5)
if message == "pong" then
  print("established server connection!")
else
  print("could not establish connection with host")
end
--load main screen
Lyqyd #2
Posted 19 October 2014 - 03:41 AM
I'd have to look at exactly what the rednet coroutine does when it gets the lookup requests, but the rednet.lookup line may be where that's coming from. Try removing it and you may see your message come through first instead of the dns lookup.
souper123 #3
Posted 19 October 2014 - 11:02 PM
I'd have to look at exactly what the rednet coroutine does when it gets the lookup requests, but the rednet.lookup line may be where that's coming from. Try removing it and you may see your message come through first instead of the dns lookup.

I tried just doing a broadcast instead of a lookup and send, but the server still outputs the message as a table.
Dragon53535 #4
Posted 20 October 2014 - 12:55 AM
Lua tables are different from say Java tables, we start at 1, not 0 so your might want to try

print(message[1])
And see how that works
souper123 #5
Posted 20 October 2014 - 02:37 AM
Lua tables are different from say Java tables, we start at 1, not 0 so your might want to try

print(message[1])
And see how that works
I have already tried that both ways, still blank. I literally have no idea why this is not working.
souper123 #6
Posted 20 October 2014 - 03:35 AM
I'd have to look at exactly what the rednet coroutine does when it gets the lookup requests, but the rednet.lookup line may be where that's coming from. Try removing it and you may see your message come through first instead of the dns lookup.
I think I did it wrong the first time, but yes, rednet.lookup seem to be the issue here.
Dragon53535 #7
Posted 20 October 2014 - 04:13 AM
I know your problem, rednet.lookup is broadcasting a rednet message which is being caught by your rednet.receive.

Each table sent looks like this.

message = { sType = "lookup", sHostname = "bankServer",sProtocol = "bank" }

Also i don't believe rednet.host is sending back the data as it should, it seems this might be a bug on Dan's part. (mind you this is present in 1.63 so i'm not sure about more recent ones)
Edited on 20 October 2014 - 02:14 AM
Bomb Bloke #8
Posted 20 October 2014 - 04:35 AM
If memory serves, the rednet.run function (which runs in parallel with CraftOS on all systems when they boot) should catch lookup requests, ensuring that rednet.receive never sees them.

I'll look into this a bit later, but I'm sorta suspecting another computer in your network might be supplying the odd messages.

In the meantime, perhaps try something like textutils.pagedTabulate() on your mystery table.
Lyqyd #9
Posted 20 October 2014 - 07:40 AM
I seem to recall that rednet.run actually handles the lookup requests as rednet_message events, which would require it to have already processed and queued the event. A quick read through the rednet API should clear up any doubts in either direction.
Bomb Bloke #10
Posted 20 October 2014 - 08:08 AM
Yep, you're right - for reasons that aren't clear to me, on receiving a modem_message event that forms a lookup request, instead of just answering it the rednet.run function re-queues it as a rednet_message event and then pulls and answers that.

Because the lookup is being handled in its own co-routine, rednet.run() doesn't prevent the rednet.receive() call in the server script from pulling the lookup request message as well. Since the server script is only rigged to accept one message, the "ping" message from the client goes off into the void.

Thus this version of the server script should do the trick:

local modemSide, id, message, protocol = "top"
rednet.open(modemSide)
rednet.host("bank", "bankServer")

repeat
	id, message, protocol = rednet.receive()  -- Keep pulling rednet messages...
until message == "ping"                           -- ... until we get the one we want.

print("received ping from ID: " .. id)
rednet.send(id, "pong")

rednet.unhost("bank", "bankServer")
rednet.close(modemSide)

Edit:

The only reason I can think of for Dan to create these "useless" lookup-related rednet_message events is to specifically create this sort of scenario: where scripts that don't specifically check that messages they receive are intended for them, will bug out.

With the same update he added the protocol system, which is basically a method of filtering such messages - maybe he was trying to encourage its use? Dunno.

In any case, let's say you wanted to use a protocol to deal with this. Your code would look something like so:

Client:
local modemSide = "top"
local bprotocol = "bankServer"

rednet.open(modemSide)
local serverID = rednet.lookup(bprotocol, "bank")

print("sending ping to server ID: " ..serverID)
rednet.send(serverID, "ping", bprotocol)  -- Message sent with "bankServer" protocol

print("Waiting for server pong...")
local id, message = rednet.receive(bprotocol)  -- Messages without the "bankServer" protocol will be ignored

if message == "pong" then
  print("established server connection!")
else
  print("could not establish connection with host")
end
--load main screen

Server:
local modemSide, id, message = "top"
local bprotocol = "bankServer"

rednet.open(modemSide)
rednet.host(bprotocol, "bank")

while true do  -- 'cause why not?
	id, message = rednet.receive(bprotocol)
	
	if message == "ping" then
		print("Received ping from ID: " .. id)
		rednet.send(id, "pong", bprotocol)
	else
		print("Received odd message from ID: " .. id)
		print("It said:")
		print(message)
	end
end

rednet.unhost(bprotocol, "bank")
rednet.close(modemSide)
Edited on 20 October 2014 - 09:44 AM