Skip to content

Commit 1e9d9db

Browse files
committed
proper handling of client close on socket:receive
1 parent 8250041 commit 1e9d9db

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

ljsocket.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,12 @@ do
14721472
return len, err, num
14731473
end
14741474

1475+
if len == 0 then
1476+
-- Connection closed gracefully by remote end (FIN received)
1477+
if self.debug then print(tostring(self), ": connection closed by peer") end
1478+
return nil, "closed", 0
1479+
end
1480+
14751481
if len > 0 then
14761482
if self.debug then print(tostring(self), ": received ", len, " bytes") end
14771483

test/run.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require("test.dns_lookup_test")
22
require("test.tcp_client_test")
3+
require("test.tcp_client_server")
34

45
require("examples.dns_lookup")
56
require("examples.tcp_client_blocking")
67
require("examples.tcp_client_nonblocking")
7-
require("examples.tcp_client_server")
8-
require("examples.udp_client_server")
8+
--require("examples.udp_client_server")
Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local socket = require("ljsocket")
22
local port = 8080
3+
local update_server
4+
local update_client
35

46
do -- server
57
local info = assert(
@@ -11,6 +13,7 @@ do -- server
1113
assert(server:set_blocking(false))
1214
assert(server:bind(info))
1315
assert(server:listen())
16+
print("server: listening on port", port)
1417
local current_client = nil
1518

1619
function update_server()
@@ -19,18 +22,23 @@ do -- server
1922
if client then
2023
current_client = client
2124
assert(client:set_blocking(false))
25+
print("server: client connected")
2226
elseif err ~= "timeout" then
2327
error(err)
2428
end
2529

2630
if current_client then
2731
local str, err = current_client:receive()
28-
2932
if str then
33+
print("server: received", str)
3034
assert(str == "hello")
31-
current_client:send(str)
35+
assert(current_client:send(str))
36+
print("server: sent", str)
3237
elseif err == "closed" then
33-
current_client:close()
38+
print("server: client disconnected")
39+
assert(current_client:close())
40+
current_client = nil
41+
return true
3442
elseif err ~= "timeout" then
3543
error(err)
3644
end
@@ -42,18 +50,22 @@ do -- client
4250
local client = assert(socket.create("inet", "stream", "tcp"))
4351
assert(client:connect("localhost", port))
4452
assert(client:set_blocking(false))
53+
print("client: connected to server on port", port)
4554
local sent_message = false
4655

4756
function update_client()
4857
if client:is_connected() then
4958
if not sent_message then
59+
print("client: sending ", "hello")
5060
assert(client:send("hello"))
5161
sent_message = true
5262
else
53-
local data, err = client:receive()
63+
local str, err = client:receive()
5464

55-
if data then
56-
assert(data == "hello")
65+
if str then
66+
print("client: received", str)
67+
assert(str == "hello")
68+
print("client: closing")
5769
client:close()
5870
elseif err ~= "timeout" then
5971
error(err)
@@ -63,7 +75,13 @@ do -- client
6375
end
6476
end
6577

66-
while true do
67-
update_server()
78+
local iterations = 0
79+
while iterations < 1000 do
6880
update_client()
81+
if update_server() then break end
82+
iterations = iterations + 1
6983
end
84+
85+
if iterations == 1000 then
86+
error("test did not complete in time")
87+
end

0 commit comments

Comments
 (0)