我尝试在我的应用程序中绑定多播端口。以前代码总是有效,但在这个服务器上它(经常,但不总是)失败......
错误消息是Address already in use
,我不太明白,因为可以绑定来自多个应用程序(甚至来自同一个应用程序)的相同地址......
什么可能导致这种情况?我知道有人会要求它,所以这里是代码:
int fd = socket(PF_INET, SOCK_DGRAM, 0);
/* yes, that's a valid socket, verified.... */
u_int val = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0) {
perror("Reusing ADDR failed");
exit(1);
}
struct sockaddr_in saddr;
saddr.sin_family = PF_INET;
saddr.sin_addr.s_addr = inet_addr(multicast_group_ip);
saddr.sin_port = htons(port);
/* yes, valid multicast ip address and port, verified */
if(bind(fd, (struct sockaddr *) &saddr, sizeof(struct sockaddr_in)) == -1)
//FAILS....