Hello!
The call recvfrom reads messages from udp socket one by one, so to read 10 messages you should call recvfrom 10 times. Are there any methods or, may be, some low-level commands to read all the messages at once into a buffer?
Hello!
The call recvfrom reads messages from udp socket one by one, so to read 10 messages you should call recvfrom 10 times. Are there any methods or, may be, some low-level commands to read all the messages at once into a buffer?
UDP is about datagrams. It has no conception above that, especially not how to concat datagrams nor how to distinguish them after concatenation.
However, it is not clear to me *why* you want to do so. What does "all the messages" mean exactly? A sequence of messages from the same source? A soap of messages from different sources? What should happen if one of them is missed on transmission? Such questions need to be handled as well.
IMHO we're speaking of stuff belonging to the application protocol layer!?
I've never tried WSARecvFrom, as stated. However, recvmmsg certainly works for datagrams from different addresses. It returns a pointer to a return-value sized array of mmsghdr structures, each of which contains a msg_hdr structure and the receive length for that particular message. The msg_hdr structure contains a member msg_name, which is the originating address.WSARecvFrom allows multiple buffers for purposes of scatter/gather, but only returns one message at a time. Also, this is pretty clear, because it only returns one socket address. Similarly, recvmmsg() only really makes sense for connected sockets, because it, also, doesn't return the addresses of any of the messages.
For the common use case of a UDP server talking to more than one client on the same socket, neither of those functions are appropriate.
The msg_hdr structure contains a member msg_name, which is the originating address
Hello!
The call recvfrom reads messages from udp socket one by one, so to read 10 messages you should call recvfrom 10 times. Are there any methods or, may be, some low-level commands to read all the messages at once into a buffer?
I think you're asking the wrong question. My guess is you worry about multiple clients sending UDP messages to a server, and you won't be able to read them all out in a timely manner for some reason.
I think what you really want to do is read UDP messages as they arrive (considering you won't really have 10 UDP packets arriving at the "same time"), so you should use select() (or some other method of notification) to signal you have data to be read from the UDP socket. At that point, it's just a matter of calling recvfrom() as you get notified of a packet arriving, and determining which client sent the data.
My Gamedev Journal: 2D Game Making, the Easy Way
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
It depends on the throughput expected.
...
The question is whether the OP is at that scale or not -- we have no way of knowing.
hplus, you know more about networking than me, no doubt, but, reading his initial question, I feel pretty safe in assuming he isn't in need of that kind of throughput and it's is more of a beginner networking question.
Either way, we now have both bases covered.
My Gamedev Journal: 2D Game Making, the Easy Way
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
There is some middle processing (often done in a separate thread) where all the "Reliability" issues (if any) are handled and channeling/organizing info down different processing streams (ie- looking for client signals like close/byebye msgs versus the running data streams, or multiple packet block completion ) can be done before it gets to the higher level game processing.
If you have multiple packets making up one message then you need data markings within the packets to allow the receiver to determine if the whole message HAS arrived (intact) to then be passed-on and actioned (at higher level or whatever).