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/tcp/file_server.lua | |
| parent | a6e3140520f31c756cc8982d3ab20eff95b75160 (diff) | |
add sendfile demo
Diffstat (limited to 'tests/modules/socket/tcp/file_server.lua')
| -rw-r--r-- | tests/modules/socket/tcp/file_server.lua | 24 |
1 files changed, 24 insertions, 0 deletions
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 |
