Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tArgs = {...}
- local x, y = term.getCursorPos()
- function printUsage()
- print("Usage:")
- print("sockconnect <side> <ip> <port>")
- end
- function threadRead()
- while true do
- local tmp = socket.readLine(tArgs[1])
- term.setCursorPos(x,y)
- if tmp == nil then
- exitmsg = "Remote Host Closed Connection."
- return false
- end
- print(">> "..tmp)
- x, y = term.getCursorPos()
- term.setCursorPos(1,1)
- print ("> "..spaces(getTermSize("x")-2))
- end
- end
- function spaces(count)
- local ret = ""
- for i = 1, count do
- ret = ret .. " "
- end
- return ret
- end
- function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- function getTermSize(axis)
- x1, y1 = term.getSize()
- if axis == "x" then
- return x1
- else
- return y1
- end
- end
- function threadWrite()
- while true do
- term.setCursorPos(1,1)
- print (">")
- term.setCursorPos(3,1)
- local tmp2 = read()
- term.setCursorPos(1,1)
- print(spaces(getTermSize("x")))
- if tmp2 == "//exit" then exitmsg = "Connection closed." return false end
- socket.writeLine(tArgs[1], tmp2)
- --local x, y = term.getCursorPos()
- term.setCursorPos(x,y)
- print("<< "..tmp2)
- x, y = term.getCursorPos()
- term.setCursorPos(1,1)
- print ("> "..spaces(getTermSize("x")-2))
- end
- end
- function connect()
- suc, msg = socket.connect(tArgs[1], tArgs[2], tArgs[3])
- if suc then
- return true
- else
- print ("Error while connecting: "..msg)
- return false
- end
- end
- exitmsg = ""
- clear()
- if connect() then
- term.setCursorPos(1,2)
- print ("Connected. Type //exit to close connection.")
- x, y = term.getCursorPos()
- parallel.waitForAny(threadWrite, threadRead)
- socket.disconnect(tArgs[1])
- --clear()
- print (exitmsg)
- exitmsg = nil
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement