-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from seregaxvm/master
add TCP and UDP server samples
- Loading branch information
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}) |