Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 什么是;未连接:sendto();返回值意味着什么?_Lua_Udp_Return Value_Luasocket - Fatal编程技术网

Lua 什么是;未连接:sendto();返回值意味着什么?

Lua 什么是;未连接:sendto();返回值意味着什么?,lua,udp,return-value,luasocket,Lua,Udp,Return Value,Luasocket,LuaSocket文档说明: 未连接:发送到(数据报、ip、端口) 如果成功,该方法将返回1。如果出现错误,请使用该方法 返回nil,后跟错误消息 但是我得到的值是4。4的返回值是什么意思 我的代码在这里: local socket = require("socket") udp = socket.udp() udp:setsockname("*", 8080) local msg = "Test" m=assert(udp:sendto( msg, "228.192.1.1", 8080

LuaSocket文档说明:

未连接:发送到(数据报、ip、端口)

如果成功,该方法将返回1。如果出现错误,请使用该方法 返回nil,后跟错误消息

但是我得到的值是
4
4的返回值是什么意思

我的代码在这里:

local socket = require("socket")

udp = socket.udp()
udp:setsockname("*", 8080)

local msg = "Test"

m=assert(udp:sendto( msg, "228.192.1.1", 8080))
print(m)
仔细查看
udp.c
中的
sendo
方法

static int meth_sendto(lua_State *L) {
    p_udp udp = (p_udp) auxiliar_checkclass(L, "udp{unconnected}", 1);
    size_t count, sent = 0;
    const char *data = luaL_checklstring(L, 2, &count);
    const char *ip = luaL_checkstring(L, 3);
    const char *port = luaL_checkstring(L, 4);
    p_timeout tm = &udp->tm;
    int err;
    struct addrinfo aihint;
    struct addrinfo *ai;
    memset(&aihint, 0, sizeof(aihint));
    aihint.ai_family = udp->family;
    aihint.ai_socktype = SOCK_DGRAM;
    aihint.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV;
    err = getaddrinfo(ip, port, &aihint, &ai);
    if (err) {
        lua_pushnil(L);
        lua_pushstring(L, gai_strerror(err));
        return 2;
    }
    timeout_markstart(tm);
    err = socket_sendto(&udp->sock, data, count, &sent, ai->ai_addr,
        (socklen_t) ai->ai_addrlen, tm);
    freeaddrinfo(ai);
    if (err != IO_DONE) {
        lua_pushnil(L);
        lua_pushstring(L, udp_strerror(err));
        return 2;
    }
    lua_pushnumber(L, (lua_Number) sent);
    return 1;
}
基本上,文档的“returns1”语句是错误的。代码中的
return 1
语句表示实际函数返回一个值,该值实际上被推入堆栈:

lua_pushnumber(L, (lua_Number) sent);
其中变量
sent
仅通过上面的几个语句计算(检查
socket\u sendto
call)


因此,返回的
4
正好是:发送的字节数
sendto
返回的字节数