first glimmers of networking working

This commit is contained in:
Kartik K. Agaram 2021-11-21 09:58:49 -08:00
parent 15b43d3092
commit b793604279
2 changed files with 29 additions and 0 deletions

View File

@ -100,6 +100,8 @@ In addition to Lua 1.5, Teliva forks or depends on:
building text-mode user interfaces. ([Alternative documentation](https://tldp.org/LDP/lpg-0.4.pdf))
* The [lcurses](https://github.com/lcurses/lcurses) binding for ncurses.
([Documentation](http://lcurses.github.io/lcurses))
* The [luasocket](https://w3.impa.br/~diego/software/luasocket) library of
networking primitives.
* The [Kilo](https://github.com/antirez/kilo) text editor. (Read more about it
in this [fantastic walk-through](https://viewsourcecode.org/snaptoken/kilo).)

27
network.tlv Normal file
View File

@ -0,0 +1,27 @@
teliva_program = {
main = [==[
function main()
local server = assert(socket.bind("*", 8080))
server:settimeout(1)
curses.mvaddstr(1, 1, "Server bound and waiting for one request")
curses.refresh()
local available_sockets, _, error = socket.select({server}, nil)
for _, available_socket in ipairs(available_sockets) do
local client = available_socket:accept()
curses.mvaddstr(2, 1, "Connection received")
curses.refresh()
client:settimeout(1)
local line, error = client:receive()
if error then
curses.mvaddstr(3, 1, "error")
curses.refresh()
server:close()
else
curses.stdscr():mvaddstr(3, 1, "received:")
curses.stdscr():mvaddstr(4, 3, line)
curses.refresh()
end
end
curses.getch()
end]==],
}