我正在尝试在C中进行多播,但在发送UDP数据包后,我的系统丢失了所有连接并需要重新启动。
问题:
我正确解开绑定吗?
如何在IPv6中使用多播发送广播?
int family = ifa->ifa_addr->sa_family;
int socket_fd = socket(family, SOCK_DGRAM, 0);
if (setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, ifa->ifa_name, strlen(ifa->ifa_name)))
perror("setsockopt");
else
{
// do something
struct sockaddr_in6 a;
a.sin6_family = AF_INET;
a.sin6_port = htons(9000);
inet_pton(family, "ff00::0.0.0.1", &a.sin6_addr);
status = sendto(socket_fd, data, sz, 0, (struct sockaddr *)&a, sizeof(a));
}
// clean up?
if (setsockopt(socket_fd, SOL_SOCKET, SO_BINDTODEVICE, "", 0)) {
perror("setsockopt (unbind)");
}
close(socket_fd);