summaryrefslogtreecommitdiff
path: root/tests/modules/socket/tcp
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-06 22:39:09 +0800
committerruki <[email protected]>2022-04-06 22:39:09 +0800
commitcc8c6dee305ab37f2ba18bbd4d04cc62ad0a9f2e (patch)
treeb3d9c336d7beb6d72285925442aecddc004c2706 /tests/modules/socket/tcp
parent60c78f84bcef3be6e5a906cd10fe6bc0d7b0c77c (diff)
improve socket
Diffstat (limited to 'tests/modules/socket/tcp')
-rw-r--r--tests/modules/socket/tcp/echo_client.lua4
-rw-r--r--tests/modules/socket/tcp/echo_server.lua8
-rw-r--r--tests/modules/socket/tcp/file_client.lua10
3 files changed, 12 insertions, 10 deletions
diff --git a/tests/modules/socket/tcp/echo_client.lua b/tests/modules/socket/tcp/echo_client.lua
index 55613cd10..bb2853c95 100644
--- a/tests/modules/socket/tcp/echo_client.lua
+++ b/tests/modules/socket/tcp/echo_client.lua
@@ -1,3 +1,4 @@
+import("core.base.bytes")
import("core.base.socket")
function main()
@@ -8,10 +9,11 @@ function main()
if sock then
print("%s: connected!", sock)
local count = 0
+ local buff = bytes(8192)
while count < 10000 do
local send = sock:send("hello world..", {block = true})
if send > 0 then
- sock:recv(13, {block = true})
+ sock:recv(buff, 13, {block = true})
else
break
end
diff --git a/tests/modules/socket/tcp/echo_server.lua b/tests/modules/socket/tcp/echo_server.lua
index 162bc2bc0..048db0c37 100644
--- a/tests/modules/socket/tcp/echo_server.lua
+++ b/tests/modules/socket/tcp/echo_server.lua
@@ -1,3 +1,4 @@
+import("core.base.bytes")
import("core.base.socket")
function main()
@@ -13,8 +14,9 @@ function main()
print("%s: accepted", sock_client)
local count = 0
local result = nil
+ local buff = bytes(8192)
while true do
- local recv, data = sock_client:recv(13, {block = true})
+ local recv, data = sock_client:recv(buff, 13, {block = true})
if recv > 0 then
result = data
sock_client:send(data, {block = true})
@@ -24,7 +26,9 @@ function main()
end
end
print("%s: recv: %d, count: %d", sock_client, result and result:size() or 0, count)
- result:dump()
+ if result then
+ result:dump()
+ end
sock_client:close()
end
end
diff --git a/tests/modules/socket/tcp/file_client.lua b/tests/modules/socket/tcp/file_client.lua
index 92d62976b..27dccfc7d 100644
--- a/tests/modules/socket/tcp/file_client.lua
+++ b/tests/modules/socket/tcp/file_client.lua
@@ -11,13 +11,12 @@ function main()
local recv = 0
local data = nil
local wait = false
- local results = {}
+ local buff = bytes(8192)
while true do
- real, data = sock:recv(8192)
+ real, data = sock:recv(buff, 8192)
if real > 0 then
recv = recv + real
wait = false
- table.insert(results, data)
elseif real == 0 and not wait then
if sock:wait(socket.EV_RECV, -1) == socket.EV_RECV then
wait = true
@@ -28,9 +27,6 @@ function main()
break
end
end
- if #results > 0 then
- data = bytes(results)
- end
- print("%s: recv ok, size: %d, #data: %d!", sock, recv, data and data:size() or 0)
+ print("%s: recv ok, size: %d!", sock, recv)
sock:close()
end