diff options
| author | ruki <[email protected]> | 2019-10-27 21:08:19 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-10-27 21:08:19 +0800 |
| commit | 239d4dead77af769ba5adccb58f30e9d52238adc (patch) | |
| tree | 351d2e8ca05ca1daab1d79b0e8d5045bce8cbcf4 /tests/modules/socket/tcp/echo_server.lua | |
| parent | 6df3d8bf595b3645022b4d465fd1192f9033913e (diff) | |
improve socket accept and connect
Diffstat (limited to 'tests/modules/socket/tcp/echo_server.lua')
| -rw-r--r-- | tests/modules/socket/tcp/echo_server.lua | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/tests/modules/socket/tcp/echo_server.lua b/tests/modules/socket/tcp/echo_server.lua index 7c200989b..58e1956c1 100644 --- a/tests/modules/socket/tcp/echo_server.lua +++ b/tests/modules/socket/tcp/echo_server.lua @@ -1,28 +1,19 @@ import("core.base.socket") function main() + local addr = "127.0.0.1" local port = 9001 local sock = socket.bind(addr, port) sock:listen(20) - local sock_client = nil + print("%s: listening %s:%d ..", sock, addr, port) while true do - print("%s: listening %s:%d ..", sock, addr, port) - local ok = sock:wait(socket.EV_ACPT, -1) - if ok == socket.EV_ACPT then - sock_client = sock:accept() - if sock_client then - print("%s: accepted", sock_client) - local recv, data = sock_client:recv(8192) - if recv == 0 then - print("wait ..") - ok = sock_client:wait(socket.EV_RECV, -1) - print("wait %d", ok) - recv, data = sock_client:recv(8192) - end - print("%s: recv %d, %s", sock_client, recv, data) - sock_client:close() - end + local sock_client = sock:accept() + if sock_client then + print("%s: accepted", sock_client) + local recv, data = sock_client:recv(8192) + print("%s: recv %d, data: %s", sock_client, recv, data or "") + sock_client:close() end end sock:close() |
