lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It sends them on the network, but returns an error (see tcpdump output).

It returns an error when it receives them from the network.

I suspect this is because the code was copied from the tcp, and with
stream sockets, sending zero bytes is a no-op, and receive()/read()
returning zero bytes is an end-of-stream indicator. Neither of these
are true with datagram sockets.

Code:

require"socket"

s = assert(socket.udp())

assert(s:setsockname("*", 9876))

c = assert(socket.udp())

function send(msg)
    print("send", c:sendto(msg, "127.0.0.1", 9876))
    print("recv", s:receive())
end

send"msg"
send""
send"msg"


output:

% lua udpz.lua
send    3
recv    msg
send    nil     refused
recv    nil     refused
send    3
recv    msg


tcpdump:

sudo tcpdump -A -i lo port 9876
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 96 bytes
14:03:22.992563 IP localhost.42968 > localhost.9876: UDP, length 3
E.....@.@.<...........&.....msg
14:03:22.992642 IP localhost.42968 > localhost.9876: UDP, length 0
E.....@.@.<...........&.....

  ^--- note luasocket claims failure, but packet is sent

14:03:22.993203 IP localhost.42968 > localhost.9876: UDP, length 3
E.....@.@.<...........&.....msg


strace:

sendto(4, "msg", 3, 0, {sa_family=AF_INET, sin_port=htons(9876),
sin_addr=inet_addr("127.0.0.1")}, 16) = 3
write(1, "send\t3\n", 7send     3
)                = 7
recvfrom(3, "msg", 8192, 0, NULL, NULL) = 3
write(1, "recv\tmsg\n", 9recv   msg
)              = 9
sendto(4, "", 0, 0, {sa_family=AF_INET, sin_port=htons(9876),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
write(1, "send\tnil\trefused\n", 17send nil     refused
)    = 17
  ^-------- note system call returns succes, luasocket claims failure

recvfrom(3, "", 8192, 0, NULL, NULL)    = 0
write(1, "recv\tnil\trefused\n", 17recv nil     refused
)    = 17

  ^-------- note system call returns succes, luasocket claims failure