Advertisement
Stiepen

Untitled

Aug 29th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.78 KB | None | 0 0
  1. tArgs = {...}
  2. local x, y = term.getCursorPos()
  3. function printUsage()
  4.   print("Usage:")
  5.   print("sockconnect <side> <ip> <port>")
  6. end
  7.  
  8. function threadRead()
  9.   while true do
  10.     local tmp = socket.readLine(tArgs[1])
  11.     term.setCursorPos(x,y)
  12.     if tmp == nil then
  13.       exitmsg = "Remote Host Closed Connection."
  14.       return false
  15.     end
  16.     print(">> "..tmp)
  17.     x, y = term.getCursorPos()
  18.     term.setCursorPos(1,1)
  19.     print ("> "..spaces(getTermSize("x")-2))
  20.   end
  21. end
  22.  
  23. function spaces(count)
  24.   local ret = ""
  25.   for i = 1, count do
  26.     ret = ret .. " "
  27.   end
  28.   return ret
  29. end
  30.  
  31. function clear()
  32.   term.clear()
  33.   term.setCursorPos(1,1)
  34. end
  35.  
  36. function getTermSize(axis)
  37.   x1, y1 = term.getSize()
  38.   if axis == "x" then
  39.     return x1
  40.   else
  41.     return y1
  42.   end
  43. end
  44.  
  45. function threadWrite()
  46.   while true do
  47.     term.setCursorPos(1,1)
  48.     print (">")
  49.     term.setCursorPos(3,1)
  50.     local tmp2 = read()
  51.     term.setCursorPos(1,1)
  52.     print(spaces(getTermSize("x")))
  53.     if tmp2 == "//exit" then exitmsg = "Connection closed." return false end
  54.     socket.writeLine(tArgs[1], tmp2)
  55.     --local x, y = term.getCursorPos()
  56.     term.setCursorPos(x,y)
  57.     print("<< "..tmp2)
  58.     x, y = term.getCursorPos()
  59.     term.setCursorPos(1,1)
  60.     print ("> "..spaces(getTermSize("x")-2))
  61.   end
  62. end
  63.  
  64. function connect()
  65.   suc, msg = socket.connect(tArgs[1], tArgs[2], tArgs[3])
  66.   if suc then
  67.     return true
  68.   else
  69.     print ("Error while connecting: "..msg)
  70.     return false
  71.   end
  72. end
  73. exitmsg = ""
  74. clear()
  75. if connect() then
  76.   term.setCursorPos(1,2)
  77.   print ("Connected. Type //exit to close connection.")
  78.   x, y = term.getCursorPos()
  79.   parallel.waitForAny(threadWrite, threadRead)
  80.   socket.disconnect(tArgs[1])
  81.   --clear()
  82.   print (exitmsg)
  83.   exitmsg = nil
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement