先贴我测试用的client和server
--client
local socket = require("socket")
local host = "127.0.0.1"
local port = 12345
local sock = assert(socket.connect(host, port))
sock:settimeout(3)
print("Press enter after input something:")
local input, recvt, sendt, status
input = "hyc"
if #input > 0 then
assert(sock:send(input .. "\n"))
end
while true do
recvt, sendt, status = socket.select({sock}, nil, 1)
print(sock:getstats())
while #recvt > 0 do
print(sock:getstats())
local response, receive_status = sock:receive("*a")
if receive_status ~= "closed" then
if response then
print("@hyc response ="..response)
recvt, sendt, status = socket.select({sock}, nil, 1)
end
else
break
end
end
end
```
--server
local socket = require("socket")
local host = "127.0.0.1"
local port = "12345"
local server = assert(socket.bind(host, port, 1024))
server:settimeout(0)
local client_tab = {}
local conn_count = 0
print("Server Start " .. host .. ":" .. port)
while 1 do
local conn = server:accept()
if conn then
conn_count = conn_count + 1
client_tab = conn
print("A client successfully connect!")
end
for conn_count, client in pairs(client_tab) do
local recvt, sendt, status = socket.select({client}, nil, 1)
if #recvt > 0 then
local receive, receive_status = client:receive()
if receive_status ~= "closed" then
if receive then
assert(client:send("Client " .. conn_count .. " Send : "))
assert(client:send(receive))
print("Receive Client " .. conn_count .. " : ", receive)
end
else
table.remove(client_tab, conn_count)
client:close()
print("Client " .. conn_count .. " disconnect!")
end
end
end
end
```
我选用了receive("*a")的方式获得服务端的返回值
然后我去vs里面调试,发现第一次可以读到buf里面的数据,而且都读完了
当第二次去读的buf里面的数据的时候,recv就返回-1,错误类型是10035它会阻塞的去读
按我的想法是第一次就把所有的数据都读完了,第二次应该返回0的吧?-1的话是阻塞的去读,但是里面没有数据了,服务端也不会再发了,所以会有问题。
请教怎么解决?
相反recvline就不会反复去buffer_get获得里面的数据

