summaryrefslogtreecommitdiff
path: root/tests/modules/socket
diff options
context:
space:
mode:
Diffstat (limited to 'tests/modules/socket')
-rw-r--r--tests/modules/socket/tcp/file_client.lua30
-rw-r--r--tests/modules/socket/tcp/file_server.lua24
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