简体   繁体   English

在C中的同一主机和端口上进行多播侦听

[英]Multicast listening on same host and port in C

I have a program that listens for multicasts and I'd like it to be able to share a port with other instances of the same program, so that if I have 3 instances running on the same host, they can all share the same port. 我有一个侦听多播的程序,我希望它能够与同一程序的其他实例共享端口,因此,如果我在同一主机上运行3个实例,则它们都可以共享同一端口。

Is it possible to do this, would it involve setsockopt and SO_REUSEADDR? 是否有可能这样做,是否涉及setsockopt和SO_REUSEADDR?

EDIT: I am going off of this site for my code, the only thing I've changed is what is sent in the message. 编辑:我要离开我的代码的站点 ,我唯一更改的是消息中发送的内容。

setsockopt was what I needed, here is an example of what I did: setsockopt是我所需要的,这是我所做的一个示例:

in the declarations: 在声明中:

int yes = 1;

then before the call to bind() i called setsockopt() 然后在调用bind()之前,我调用了setsockopt()

setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

this now allows me to run multiple instances of the program on the same machine, listening for multicasts on the same port. 现在,这使我可以在同一台计算机上运行该程序的多个实例,在同一端口上侦听多播。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM