Skip to content

Commit

Permalink
Merge pull request #243 from seregaxvm/master
Browse files Browse the repository at this point in the history
add TCP and UDP server samples
  • Loading branch information
psychon authored Apr 9, 2020
2 parents ff50e59 + 7b81613 commit 9541863
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
51 changes: 51 additions & 0 deletions samples/tcpsrv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /usr/bin/env lua

--
-- Sample TCP echo server. Listens on 3333 port
-- Use netcat to send strings to server (ncat 127.0.0.1 3333)
--

local lgi = require 'lgi'
local Gio = lgi.Gio
local assert = lgi.assert

local app = Gio.Application{application_id = 'org.lgi.samples.tcptest',
flags = Gio.ApplicationFlags.NON_UNIQUE}

local service = Gio.SocketService.new()
service:add_address(
Gio.InetSocketAddress.new_from_string("127.0.0.1", 3333),
Gio.SocketType.STREAM,
Gio.SocketProtocol.TCP)

local function do_echo_connection(conn, istream, ostream)
local wrote, err = ostream:async_write_all("Connected\n")
assert(wrote >= 0, err)
while true do
local bytes = assert(istream:async_read_bytes(4096))
if bytes:get_size() == 0 then break end
local data = bytes.data:gsub("\n", "")
print(string.format("Data: %s", data))
wrote, err = ostream:async_write_all(bytes.data)
assert(wrote >= 0, err)
end
print("Closing connection")
conn:async_close()
end

function service:on_incoming(conn)
local istream = assert(conn:get_input_stream())
local ostream = assert(conn:get_output_stream())
local rc = conn:get_remote_address()
print(string.format("Incoming connection from %s:%s",
rc:get_address():to_string(),
rc:get_port()))
Gio.Async.start(do_echo_connection)(conn, istream, ostream)
return false
end

function app:on_activate()
app:hold()
end

app:run({arg[0]})
46 changes: 46 additions & 0 deletions samples/udpsrv.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#! /usr/bin/env lua

--
-- Sample UDP server. Listens on 3333 port
-- Use netcat to send strings to server (ncat -u 127.0.0.1 3333)
--

local lgi = require 'lgi'
local core = require 'lgi.core'
local GLib = lgi.GLib
local Gio = lgi.Gio
local assert = lgi.assert

local app = Gio.Application{application_id = 'org.lgi.samples.udptest',
flags = Gio.ApplicationFlags.NON_UNIQUE}

local socket = Gio.Socket.new(Gio.SocketFamily.IPV4,
Gio.SocketType.DATAGRAM,
Gio.SocketProtocol.UDP)
local sa = Gio.InetSocketAddress.new_from_string("127.0.0.1", 3333)
assert(socket:bind(sa, true))

local buf = core.bytes.new(4096)
local source = assert(socket:create_source(GLib.IOCondition.IN))

source:set_callback(function()
local len, src = assert(socket:receive_from(buf))
if len > 0 then
local data = tostring(buf):sub(1, len):gsub("\n", "")
print(string.format("%s:%d %s",
src:get_address():to_string(),
src:get_port(),
data))
else
print('Failed to read data')
end
return true
end)

source:attach(GLib.MainContext.default())

function app:on_activate()
app:hold()
end

app:run({arg[0]})

0 comments on commit 9541863

Please sign in to comment.