lsalzman / enet

ENet reliable UDP networking library
MIT License
2.77k stars 673 forks source link

How to perform broadcast to LAN? #202

Closed lolriven closed 2 years ago

lolriven commented 2 years ago

From what I understand the function ENet_host_broadcast is either A: supposed to broadcast a message to all peers or B: broadcast a message to LAN. This part is ambiguous from what I can find on the internet.

When I try creating a host on ENET_HOST_BROADCAST the call fails

    ENetAddress localaddress = {0};
    localaddress.host = ENET_HOST_BROADCAST;
    localaddress.port = 1234;
    ENetHost * server = enet_host_create (& localaddress /* the address to bind the server host to */,
                                 32      /* allow up to 32 clients and/or outgoing connections */,
                                  2      /* allow up to 2 channels to be used, 0 and 1 */,
                                  0      /* assume any amount of incoming bandwidth */,
                                  0      /* assume any amount of outgoing bandwidth */);
    if(!server){
        exit(EXIT_FAILURE);
    }

I also tried using enet_address_set_host and that too failed on the call to enet_host_create. I could create a host on ENET_HOST_ANY and have the peer manually enter the IP and Port but that would be a lot of effort. What steps do I take to broadcast a message to all peers on the LAN ?

bjorn commented 2 years ago

@JadeVand Did you find an answer to your question? If so, please don't leave this issue behind on the internet without the answer.

lolriven commented 2 years ago

@bjorn Yes and no. I've realised that the issue is with my own machine, not the library itself. This exact code seems to work for other people when I give it to them to test but it will not work for me. And I believe it has something to do with how my machine handles loopback and broadcast. The question posed above is technically incorrect, because whether it works or not I believe is machine dependant not library dependant.

bjorn commented 2 years ago

@JadeVand Hmm, that might be, but now that I read the documentation, I think you're using ENET_HOST_BROADCAST wrong:

The constant ENET_HOST_BROADCAST may be used to specify the broadcast address (255.255.255.255). This makes sense for enet_host_connect, but not for enet_host_create. Once a server responds to a broadcast, the address is updated from ENET_HOST_BROADCAST to the server's actual IP address.

So, you should first create a server on the LAN, usually just using ENET_HOST_ANY, but in some cases you may need to be more specific about the network interface. Then, any client can use ENET_HOST_BROADCAST when using enet_host_connect to connect to any server on the LAN.

Then, when the server wants to send a message to all connected clients, it would call enet_host_broadcast.

lolriven commented 2 years ago

@bjorn I did end up trying that, setting my own address to ENET_HOST_ANY then trying different broadcast address for the client/connection. it was something I tested extensively for two days , using different broadcast IP addresses but I think it's as you mentioned I need "to be more specific about the network interface" because I was not able to get a broadcast on my machine.