问个luasocket的receive(“*a“)的问题

先贴我测试用的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获得里面的数据
 

劳烦哪位大神帮我看看问题所在吧?

朋友,我也遇到你说的问题了,你解决了吗

兄台,解决了吗? 我也是遇到这个问题啊。。。。解决了说一下啊。

将cocos2d-x\external\lua\luasocket\buffer.c
下的recvall修改成下面的
static int recvall(p_buffer buf, luaL_Buffer *b) {
int err = IO_DONE;
size_t total = 0;
while (err == IO_DONE) {
const char *data; size_t count;
err = buffer_get(buf, &data, &count);
//没有接收到数据,跳出循环
if (count == 0) {
if(total > 0 && err == IO_TIMEOUT){
return IO_DONE;
}
break;
}
total += count;
luaL_addlstring(b, data, count);
buffer_skip(buf, count);
}
if (err == IO_CLOSED) {
if (total > 0) return IO_DONE;
else return IO_CLOSED;
} else return err;
}