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

Rednet server & client ping

Started by Robinlemon, 29 May 2015 - 01:53 PM
Robinlemon #1
Posted 29 May 2015 - 03:53 PM
So, I have a server, there will be many clients.

The server sends out a message rapidly (this will be set to 5 seconds)
the client will respond to that message.

Its supposed to be set where if you break the client that the server will spit out that the computer has been broken in red text, if you place the client back down, it wont reply because it had already pinged and its waiting for the rednet message, I could have a while loop running and it would break when it received a message, but then how would you tell if the client has been broken or "hacked," have a timer that counts down 10 seconds?

I have the server printing to a monitor on top whether the ping was sucessful or not.
The color changes between green and lime so you can easily see the difference if the program is running or got stuck on pinging.

My setup:


Server Code:

color = colors.lime
monitor = peripheral.wrap("top")
modem_side = "back"
monitor_or_terminal = "monitor"
rednet.open(modem_side)
if monitor_or_terminal == "monitor" then
   term.redirect(monitor)
end

while true do  
   term.setTextColor(colors.magenta)
   print("Pinging Client 1")
   rednet.broadcast("C1:PING")
   event, id, message = os.pullEvent("rednet_message")
   --print(event.." "..id.." "..message)
   if message ~= nil then
	  if message == "C1 RETURN PING" then
		 term.setTextColor(color)
		 print("Client:1:Stable:ReturnPing")
	  end
   else  
	  term.setTextColor(colors.red)
	  print("No message recieved, client 1 crashed or hacked")
   end
   if color == colors.green then
	  color = colors.lime
   elseif color == colors.lime then
	  color = colors.green	
   end
   sleep(0.1)  
end

The client code:


rednet.open("top")
while true do
   event, id, message = os.pullEvent("rednet_message")
   if message == "C1:PING" then
	  term.setTextColor(colors.orange)
	  print("Recieved message from server, responding ping request")
	  rednet.broadcast("C1 RETURN PING")
   end
end

Yes, I know that the setTextColor should be outside the loop, but i used to print out other things and it had to keep setting the color back to orange, this could had been handled in a function but for some reason I didn't do that :P/>
Edited on 29 May 2015 - 02:03 PM
flaghacker #2
Posted 29 May 2015 - 04:00 PM
And what's your problem exactly?
Creator #3
Posted 29 May 2015 - 04:03 PM
If you want a real networking API, take a look at ccTCP.

It still is not ready.

But soon.
Robinlemon #4
Posted 29 May 2015 - 04:09 PM
And what's your problem exactly?

The problem is detecting whether the client is broke or not.

If you want a real networking API, take a look at ccTCP.

It still is not ready.

But soon.

Is this working through http to remote devices such as my phone or through rednet to other computers or 'clients'
Creator #5
Posted 29 May 2015 - 04:11 PM
It uses rednet exclusively. Layer 1 and 2 work. We are working on layer 3 currenlty.
KingofGamesYami #6
Posted 29 May 2015 - 04:11 PM
The client cannot know if it's broken, as when it is broken all programs that were running are killed.

However, the server could have a timeout, which triggers an error message when the client doesn't respond.

EG:

local t = os.startTimer( 1 ) --#start a timer
while true do
   event, id, message = os.pullEvent()
   --print(event.." "..id.." "..message)
   if event == "rednet_message" and message ~= nil then
          if message == "C1 RETURN PING" then
                 term.setTextColor(color)
                 print("Client:1:Stable:ReturnPing")
          end
          break --#exit the loop
   elseif event == "timer" and id == t then --#timout (triggered after 1 second)
          term.setTextColor(colors.red)
          print("No message recieved, client 1 crashed or hacked")
          break --#exit the loop
   end
end
Edited on 29 May 2015 - 02:12 PM
Robinlemon #7
Posted 29 May 2015 - 04:36 PM
The client cannot know if it's broken, as when it is broken all programs that were running are killed.

However, the server could have a timeout, which triggers an error message when the client doesn't respond.

EG:

local t = os.startTimer( 1 ) --#start a timer
while true do
   event, id, message = os.pullEvent()
   --print(event.." "..id.." "..message)
   if event == "rednet_message" and message ~= nil then
		  if message == "C1 RETURN PING" then
				 term.setTextColor(color)
				 print("Client:1:Stable:ReturnPing")
		  end
		  break --#exit the loop
   elseif event == "timer" and id == t then --#timout (triggered after 1 second)
		  term.setTextColor(colors.red)
		  print("No message recieved, client 1 crashed or hacked")
		  break --#exit the loop
   end
end

Thanks, lock the topic