多播程式設計範例
下列範例程式碼說明如何使用通訊端選項,將多播功能納入 Windows Sockets 應用程式。
#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));
}