1、实验六、多播发送与接收程序设计一、参考程序发送程序:/各调用函数定义 /本地接口 local_if 加入多播组 mcaddrint mc_join(SOCKET s,struct in_addr *mcaddr, struct in_addr *local_if)struct ip_mreq mreq;/首先用 memcpy 函数构造 ip_mreq 型的结构变量 mregmemcpy( / local ifmemcpy( / mutilcast group addressreturn (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)
2、/ 为多播报文设置外出接口int mc_setIF(SOCKET s, const DWORD local_out_if)return (setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, (char *)/ 设置外出多播报文的 ttl 值,默认为 1int mc_setTTL(SOCKET s, const DWORD ttl)return (setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char *)/ 设置外出多播报文是否返回,默认为 trueint mc_setLoop(SOCKET s,const BOOL
3、 flag)return (setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,(char *)flag,sizeof(flag);int mc_leave(SOCKET s,struct in_addr *mcaddr,struct in_addr *local_if)/指定成员推出指定多播组,为了通用性。一般程序运行结束会自动离开)struct ip_mreq mreq;/构造 ip_mreq 结构变量memcpy(memcpy(return(setsockopt(s,IPPROTO_IP,IP_DROP_MEMBERSHIP,(char *)/主程序voi
4、d main() WSADATA wsaData; WSAStartup(MAKEWORD(2,2), SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);struct sockaddr_in to;memset(to.sin_family=AF_INET; to.sin_port=htons(9999); to.sin_addr.S_un.S_addr=inet_addr(INADDR_ANY); bind(sock,(struct sockaddr *)struct in_addr mcaddr;mcaddr.s_addr =inet_addr(“226.
5、1.2.3“);mc_join(sock,char buf65 ;while(1)memset(buf,0,65);recvfrom(sock,buf,64,0,NULL,NULL);printf(“recvd: %sn“,buf);发送程序:#pragma comment(lib,“ws2_32.lib“) #include #includevoid main(int argc,char* argv) WSADATA wsaData; WSAStartup(MAKEWORD(2,2), SOCKET sock = socket(AF_INET,SOCK_DGRAM,0);DWORD ttl;ttl=219;struct sockaddr_in to; / int len = sizeof(to);memset( /初始化数组to.sin_family=AF_INET; to.sin_port=htons(9999); to.sin_addr.S_un.S_addr=inet_addr(“226.1.2.3“); char *msg = “大家好!我是# _“;while(1)int res = sendto(sock,msg,60,0,(struct sockaddr *)二、实验报告要求用抓图方法表示实验结果