Advertisement
DOGGYWOOF

server w connection request

Jan 7th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. local modemSide = "top" -- Modify with your modem side
  2.  
  3. rednet.open(modemSide)
  4. print("Server online. Waiting for connection...")
  5.  
  6. local function acceptConnection()
  7. while true do
  8. local senderId, message = rednet.receive()
  9. if message == "connect" then
  10. print("Incoming connection from ID: " .. senderId)
  11. print("Do you want to accept this connection? (yes/no)")
  12. local input = read()
  13. if input == "yes" then
  14. rednet.send(senderId, "accepted")
  15. return senderId
  16. else
  17. rednet.send(senderId, "rejected")
  18. end
  19. end
  20. end
  21. end
  22.  
  23. local function startShell(clientId)
  24. print("Connection accepted. Starting shell.")
  25. print("Type 'exit' to disconnect.")
  26. while true do
  27. io.write("> ")
  28. local input = read()
  29. if input == "exit" then
  30. rednet.send(clientId, "exit")
  31. print("Shell closed.")
  32. return
  33. else
  34. rednet.send(clientId, input)
  35. local _, message = rednet.receive(5) -- Timeout of 5 seconds
  36. if message then
  37. print(message)
  38. else
  39. print("No response from client.")
  40. end
  41. end
  42. end
  43. end
  44.  
  45. local clientId = acceptConnection()
  46. if clientId then
  47. startShell(clientId)
  48. end
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement