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

rednet.receive() getting more than one response

Started by alez290, 25 January 2015 - 07:06 PM
alez290 #1
Posted 25 January 2015 - 08:06 PM
Okay so this is a little complicated, I have these computers that run servers. This means that I have other computers that can log into the server. But in order to log into a server the user needs to know the server ID. What i want is for computers to be able to do a broadcast command then listen for any responses from servers and tell the user all the server IDs in the area. some sudo code here-

function getServers()
rednet.broadcast("whatsYourID")

recieves ID from all servers in range

print server IDs
end

I am totally stumped, anything I can do?
HPWebcamAble #2
Posted 25 January 2015 - 11:04 PM
I prefer to interface with modems using the Modem API, rather than the rednet API. I guess its just a personal preference



m = peripheral.wrap("top") --# assuming your modem is on the top
m.open(1) --# uses channel 1, but there are like 25600 so you can use whatever you want

function getServers()

  m.transmit(1,1,"SeverLocate") --# asks for severs
  timer = os.startTimer(2) --# waits 2 seconds

  while true do

	event,p1,p2,p3,p4,p5 = os.pullEvent()
	if event == "timer" and p1 == timer then --#if time is up
	  break
	elseif event == "modem_message" then --#if a message was recieved
	  if p4 == "ServerHere" then
		--# a server responded, do stuff
	  end
	end

  end

end


'modem_message' arguments are name, channel, replyChannel, message, and distance to sender

You can have the server and client send anything, like a table with information.
Edited on 25 January 2015 - 10:06 PM
alez290 #3
Posted 26 January 2015 - 01:15 AM
oh wow that's perfect, thanks!
Lyqyd #4
Posted 26 January 2015 - 02:29 AM
Of course, the rednet API has a built-in function to do this sort of thing, if you're using a version of ComputerCraft 1.6 or later. The rednet.lookup function can find all computers in range that have called rednet.host with a given protocol.