shiskebab

CC client server

Jul 24th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.32 KB | None | 0 0
  1. CLIENT:::::::
  2.  
  3. local compid
  4. local alv3
  5.  
  6. local function Startup()
  7.   term.clear()
  8.   term.setCursorPos (1,1)
  9.   rednet.open("right")
  10.   print ("   this Computer's id: ", os.getComputerID())
  11.   print ("   Set receiving computer's id:")
  12.   print ("   +--------------------------+   ")
  13.   print ("                              |   ")
  14.   print ("   +--------------------------+   ")
  15.   print ()
  16.   print ("   message:")
  17.   print ("   +--------------------------+   ")
  18.   print ("                              |   ")
  19.   print ("   +--------------------------+   ")
  20.   term.setCursorPos(5,4)
  21.   repeat
  22.         compid = tonumber(read())
  23.   until compid
  24.   term.setCursorPos(5,4)
  25.   print ("id saved")
  26.   term.setCursorPos(5,9)
  27.   alv3 = read()
  28.   term.setCursorPos(5,9)
  29.   print ("message saved")
  30.   sleep(0.5)
  31.   Sender()
  32.   Receive()
  33. end
  34.  
  35. function Sender()
  36.   term.clear()
  37.   term.setCursorPos (1,1)
  38.   print ("sending message in: ")
  39.   print ("3")
  40.   sleep(1)
  41.   print ("2")
  42.   sleep(1)
  43.   print ("1")
  44.   sleep(1)
  45.   rednet.send(compid, alv3)
  46.   sleep(1)
  47.   print ("message sent")
  48.   sleep(1)
  49. end
  50.  
  51. function Receive()
  52.   term.clear()
  53.   term.setCursorPos (1,1)
  54.   print ("   this Computer's id: ", os.getComputerID())
  55.   print ("   waiting for message:")
  56.   print ("   +--------------------------+   ")
  57.   print ("                              |   ")
  58.   print ("   +--------------------------+   ")
  59.   print ()
  60.   print ("   Terminal:")
  61.   print ("   +--------------------------+   ")
  62.   print ("                              |   ")
  63.   print ("   +--------------------------+   ")
  64.  
  65.   while true do
  66.    rednet.open("right")
  67.    senderID, message = rednet.receive()
  68.      term.setCursorPos (5,4)
  69.      print (message)
  70.      sleep(0.5)
  71.      term.setCursorPos (5,9)
  72.      print (senderID)
  73.      local event = os.pullEvent("key")
  74.      rednet.close("right")
  75.      Startup()
  76.      break
  77.   end
  78.   end
  79.  
  80. Startup()
  81.  
  82.  
  83. SERVER :::::::
  84.  
  85. local compid
  86.  
  87. local function Startup()
  88.   term.clear()
  89.   term.setCursorPos (1,1)
  90.   print ("   this Computer's id: ", os.getComputerID())
  91.   print ("   Set this Computer's label:")
  92.   print ("   +--------------------------+   ")
  93.   print ("                              |   ")
  94.   print ("   +--------------------------+   ")
  95.   print ()
  96.   print ("   Set The Computer Terminal's ID")
  97.   print ("   +--------------------------+   ")
  98.   print ("                              |   ")
  99.   print ("   +--------------------------+   ")
  100.   term.setCursorPos(5,4)
  101.   local IgnLabel = read()
  102.   os.setComputerLabel(IgnLabel)
  103.   term.setCursorPos(5,4)
  104.   print ("Label successful")
  105.   term.setCursorPos(5,9)
  106.   repeat
  107.         compid = tonumber(read())
  108.   until compid
  109.   term.setCursorPos(5,9)
  110.   print ("ID saved")
  111.   sleep(2)
  112.   print ("storing message")
  113.   WriteFile()
  114.   sleep(1)
  115.   print ("sending stored messages")
  116.   SendFile()
  117.   print ("message sent!")
  118.   sleep(5)
  119.   Startup()
  120. end
  121.  
  122. function WriteFile()
  123.   rednet.open("right")
  124.   id, message = rednet.receive()
  125.   if id == compid then
  126.    rednet.close()
  127.     local h = fs.open("messages", "a")
  128.      h:write(message)
  129.      h:close()
  130.   else
  131.    print ("failed to write")
  132.    sleep(5)
  133.   end
  134.   end
  135.  
  136. function SendFile()
  137.   local SendIt = fs.open("messages", "r")
  138.   local shipment = SendIt.readAll()
  139.   SendIt.close()
  140.   rednet.open("right")
  141.   rednet.send(compid, shipment)
  142.   rednet.close("right")
  143. end
  144. Startup()
Advertisement
Add Comment
Please, Sign In to add comment