Page 1 of 1

Send multicast datagram

Posted: May 13th, 2020, 9:56 pm
by skidoo
Maybe stupid question but I do not get it.

Want to send an udp datagram to multicast address using MPD's internal API.

Code: Select all

SocketDescriptor MySocketDesc;
        if (!MySocketDesc.Create(AF_INET, SOCK_DGRAM, IPPROTO_IP))
        {
// error
        }
Ok there is a socket descriptor. But MySocketDesc.Write() requires SocketAddress. But how to initialise? Plain C uses sockaddr_in - what is the MPD way?

Re: Send multicast datagram

Posted: May 14th, 2020, 7:48 am
by max
A "SocketAddress" is just a wrapper for "struct sockaddr" (it even has a constructor to initialize it with a "struct sockaddr"), and the rest is normal C knowledge.
But there's also the class "IPv4Address" which is implicitly convertible to "SocketAddress", and that class may be easier for you to handle. It can be initialized with a "struct sockaddr_in", but can also be initialized with IPv4 octets plus port, no fiddling with big-endian.

MPD uses those abstractions because I found the normal socket API to be cumbersome and error prone. And not portable - MPD's classes work on Windows (WinSock) as well.

Re: Send multicast datagram

Posted: May 14th, 2020, 12:03 pm
by skidoo
But there's also the class "IPv4Address" which is implicitly convertible to "SocketAddress", and that class may be easier for you to handle. It can be initialized with a "struct sockaddr_in", but can also be initialized with IPv4 octets plus port, no fiddling with big-endian.
That's how it worked. Thank you, Max :)