Sampel Pemrograman Multicast
Kode sampel berikut menggambarkan cara menyertakan fungsionalitas multicast ke aplikasi Windows Sockets menggunakan opsi soket.
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <mswsock.h>
#define u_int32 UINT32 // Unix uses u_int32
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
int /* OUT: whatever setsockopt() returns */
join_source_group(int sd, u_int32 grpaddr,
u_int32 srcaddr, u_int32 iaddr)
{
struct ip_mreq_source imr;
imr.imr_multiaddr.s_addr = grpaddr;
imr.imr_sourceaddr.s_addr = srcaddr;
imr.imr_interface.s_addr = iaddr;
return setsockopt(sd, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *) &imr, sizeof(imr));
}
int
leave_source_group(int sd, u_int32 grpaddr,
u_int32 srcaddr, u_int32 iaddr)
{
struct ip_mreq_source imr;
imr.imr_multiaddr.s_addr = grpaddr;
imr.imr_sourceaddr.s_addr = srcaddr;
imr.imr_interface.s_addr = iaddr;
return setsockopt(sd, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, (char *) &imr, sizeof(imr));
}