Re: gen_udp:recv()

zxq9 <[email protected]>
Newsgroups gmane.comp.lang.erlang.general
Message-ID <[email protected]>
Hi Avinash,

On 2021/04/05 22:42, Avinash Dhumane wrote:
> I am taking a short-cut to inquire this, by posting to you all (than 
> going through relevant documentation).

Oh! The humanity!

> Is gen_udp:recv() to be invoked per datagram, or can it return a binary 
> of multiple datagrams concatenated?

It returns a single datagram with some meta about the transmission 
(port, address, etc.).

> I am asking this because there is Length to recv(), and I don’t 
> understand why it is there. Should it specify the length of expected 
> datagram? What if the datagrams of variable length? Should it, then, 
> specify the maximum length?

This goes back to memory allocation issues when you are allocating 
memory manually. How much is the max size you want to receive might be 
based on the size of the buffer you're willing to set aside for the 
incoming data -- not the kind of thing people have to contemplate as 
carefully as they did in earlier eras of networking.

There are some other lower level UDP dials and knobs that allow you to 
peek at the size and UDP headers and whatnot to figure out what a 
reasonable balance between getting an entire datagram, being safe, and 
not being wasteful might be -- and typically you know what protocol 
you're working with.

Anyway, in Erlang this is pretty much irrelevant. The common way to 
receive UDP traffic (and TCP traffic, for that matter) is to leave all 
the low level stuff to the runtime and inside your code just receive it 
as an Erlang message and not deal with udp:recv/2,3 at all.

-Craig
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.