diff options
| author | ruki <[email protected]> | 2019-10-28 22:40:27 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-28 10:02:46 +0800 |
| commit | 8bfbf6b7fb255cda1bfd3d970c7442ed0941104c (patch) | |
| tree | 356e02c9fece1f27909a64f6d270e0201ab270b5 /tests/modules/socket | |
| parent | a6e3140520f31c756cc8982d3ab20eff95b75160 (diff) | |
add sendfile demo
Diffstat (limited to 'tests/modules/socket')
| -rw-r--r-- | tests/modules/socket/tcp/file_client.lua | 30 | ||||
| -rw-r--r-- | tests/modules/socket/tcp/file_server.lua | 24 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua new file mode 100644 index 000000000..3d5a14651 --- /dev/null +++ b/tests/modules/socket/tcp/file_client.lua @@ -0,0 +1,30 @@ +import("core.base.socket") + +function main() + local addr = "127.0.0.1" + local port = 9090 + print("connect %s:%d ..", addr, port) + local sock = socket.connect(addr, port) + print("%s: connected!", sock) + local real = 0 + local recv = 0 + local data = nil + local wait = false + while true do + real, data = sock:recv(8192, {prevdata = data}) + if real > 0 then + recv = recv + real + wait = false + elseif real == 0 and not wait then + if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then + wait = true + else + break + end + else + break + end + end + print("%s: recv ok, size: %d!", sock, recv) + sock:close() +end diff --git a/tests/modules/socket/tcp/file_server.lua b/tests/modules/socket/tcp/file_server.lua new file mode 100644 index 000000000..d619a496f --- /dev/null +++ b/tests/modules/socket/tcp/file_server.lua @@ -0,0 +1,24 @@ +import("core.base.socket") + +function main(filepath) + + local addr = "127.0.0.1" + local port = 9090 + local sock = socket.bind(addr, port) + sock:listen(20) + print("%s: listening %s:%d ..", sock, addr, port) + while true do + local sock_client = sock:accept() + if sock_client then + print("%s: accepted", sock_client) + local file = io.open(filepath, 'rb') + if file then + local send = sock_client:sendfile(file, {block = true}) + print("%s: send %s %d bytes!", sock_client, filepath, send) + file:close() + end + sock_client:close() + end + end + sock:close() +end |
