Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
darkrising

Telnet in lua

Question

Hi, I've been working on a small telnet program, ran into a few bumps along the way.

Here's my code:

local component = require("component")
local internet = require("internet")

local host = "gcomm.com:23"

-- try to connect to server.
local sock, reason = internet.open(host)
if not sock then
 io.stderr:write(reason .. "\n")
 return
end

sock:setTimeout(0.05)

repeat
 local ok, line = pcall(sock.read, sock)
 if ok then
   if not line then
     print("Connection lost.")
     sock:close()
     sock = nil
     return false
   end

   print(line)
 end
until not sock

As you can see it simply outputs messages directly to the screen (Don't know what this host is, seems to be some type of message board or something, found it on Google :P )

In ComputerCraft I would run 2 functions in parallel one to listen and one to handle drawing and everything else (user input) but I'm not to sure how the same thing would be accomplished in OpenComputers.

Any help would be much appreciated.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I've got a step further:

local component = require("component")
local internet = require("internet")
local term = require("term")
local text = require("text")
local event = require("event")

local host = "gcomm.com:23"

local gpu = component.gpu
local w, h = gpu.getResolution()

local hist = {}

local sock, reason = internet.open(host)
if not sock then
 io.stderr:write(reason .. "\n")
 return
end

sock:setTimeout(0.05)

--Function from the built in IRC program
local function print(message, overwrite)
 local w, h = component.gpu.getResolution()
 local line
 repeat
   line, message = text.wrap(text.trim(message), w, w)
   if not overwrite then
     component.gpu.copy(1, 1, w, h - 1, 0, -1)
   end
   overwrite = false
   component.gpu.fill(1, h - 1, w, 1, " ")
   component.gpu.set(1, h - 1, line)
 until not message or message == ""
end
local function draw()
 if not sock then
   return false
 end
 repeat
   local ok, line = pcall(sock.read, sock)
   if ok then
     if not line then
       print("Connection lost.")
       sock:close()
       sock = nil
       return false
     end
     print(line)
   end
 until not ok
end
local function uin()
 term.setCursor(1,h)
 line = term.read(hist)
 line2 = text.trim(line)
 if line2 == "/exit" then
   return false
 else
   sock:write(line2.."\r\n")
 end
 return true
end

local going = true
local dLoop = event.timer(0.5, draw, math.huge)

repeat
 r = uin()
until not r

if dLoop then
 event.cancel(dLoop)
end

if sock then
 sock:write("QUIT")
 sock:close()
end

However now it seems to stops outputting the text coming from the server, not sure why.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.