1、发送:
//设置组播ttl的值,默认为1
int ttl = 1;
setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&ttl, sizeof(ttl));
2、接收:
// 加入组播啦
ip_mreq multiCast;
multiCast.imr_interface.S_un.S_addr = INADDR_ANY;
multiCast.imr_multiaddr.S_un.S_addr = inet_addr("234.2.2.2");
setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multiCast, sizeof(multiCast));
网友评论