[ENet-discuss] how to receive broadcast and reply to broadcast sender by using enet

Ruud van Gaal ruud at racer.nl
Sat Jan 12 15:38:46 PST 2013


Sending and receiving should be one the same port.
It seems these functions map quite directly to regular socket functions;
for multicast I have to do this (my own library, but it's quite a direct
mapping):

    QNAddress addr;
    sock=new QNSocket(QNSocket::UNRELIABLE);
    sock->Open(PORT,QNSocket::NO_BIND);
    sock->BindAny(PORT);
    sock->JoinMulticastGroup("226.0.0.1");
    sock->SetMulticastLoop(false);           // Don't receive our own sends
    sock->SetNonBlocking(true);
    while(1)
    {
      n=sock->Read(buf,sizeof(buf),&addr);
      if(n>0)
      {
        buf[n]=0;
        qdbg("Incoming packet %d bytes (%s).\n",n,buf);
      }
    }

Where the relevant functions are:

---
bool QNSocket::JoinMulticastGroup(cstring addr)
// Join a multi-cast group.
// Used for servers. I.e. addr="226.0.0.1"
// Use a multi-cast address (224.0.0.1 - 239.255.255.255)
{
  ip_mreq imreq;
  int status;

  memset(&imreq,0,sizeof(imreq));

  imreq.imr_multiaddr.s_addr = inet_addr(addr);
  imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface

  // JOIN multicast group on default interface
  status = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
             (char*)&imreq, sizeof(struct ip_mreq));
  return status>=0;
}
---
void QNSocket::SetMulticastLoop(bool yn)
// Enable loopback to local machine (yn=true) or not (yn=false)
// Default initial state is true
{
  int status;
  char b=yn;

status=setsockopt(fd,IPPROTO_IP,IP_MULTICAST_LOOP,(char*)&b,sizeof(unsigned
char));
}
---

Here. I call SetMulticastLoop(false) to avoid all broadcasted message to
echo back to localhost (if that's what you want though).

Cheers,
Ruud

On Sat, Jan 12, 2013 at 11:19 AM, enetcub <enet_club at 126.com> wrote:

>  HI ALL:
>
>
>
>
> once a client send broadcast to all host server in a same subnet, how can
> servers recevice the broadcast infomation and make reply to the client
> who has send the broadcast?
>
> client :
> ----------------------------------------------------------------
>
>
>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    - **
>
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>
>
>    1. void scan_servers_by_send_broadcast()
>    2. {
>    3. ENETAddress address;
>    4.
>    5.
>    6.     address.host = ENET_HOST_BROADCAST;
>    7.     address.port = BROADCAST_PORT
>    8.
>    9.
>    10. ENETSocket socket = ENET_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
>    11. ENET_socket_set_option(socket,ENET_SOCKOPT_NONBLOCK,1);
>    12. ENET_socket_set_option(socket,ENET_SOCKOPT_BROADCAST,1);
>    13.
>    14.
>    15. ENET_socket_send(socket,address,&broadcast_buf,1);
>    16. ENET_socket_destroy(socket);
>    17. }
>
>
>
> servers:
> -------------------------------------------------------------
> [Q: How can a server received broadcast information and tell client its ip
> address ?]
>
>
>
>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    - **
>
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>    <http://www.gamedev.net/topic/637167-how-to-receive-broadcast-and-reply-to-broadcast-sender-by-using-enet/#>
>    -
>
>
>    1. ENETSocket socket = ENET_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
>    2. ENET_socket_set_option(socket,ENET_SOCKOPT_NONBLOCK,1);
>    3. ENET_socket_set_option(socket,ENET_SOCKOPT_REUSEADDR,1);
>    4. ENET_socket_set_option(socket,ENET_SOCKOPT_BROADCAST,1);
>    5.
>    6.
>    7. ENETAddress serv_addr;
>    8.     serv_addr.host=ENET_HOST_ANY;
>    9.     serv_addr.port=BROADCAST_PORT;      // IS THIS RIGHT?
>    10. ENET_socket_bind(socket,&serv_addr);
>    11. ...........
>
>
> I DON'T KNOW WHAT SHOULD DO AFTER BINDING,,,,,  HELP ME PLEASE ,THANKS/...
>
> any more, does BROADCAST_PORT shold be different from the
> SERVER_MAIN_SERVICE_PORT ?
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20130113/66c25008/attachment-0001.html>


More information about the ENet-discuss mailing list