0

I am trying to broadcast messages over registered multiple client over UDP protocol.

I believe we need below two commands for sending and receiving over sockets :

sendto(sock,buffer,
            strlen(buffer),0,(const struct sockaddr *)&server,length);

recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);

As we can see , we need 6 argument for same, second last being the the address of recipient. In case of client to server , we have address. But from server to client we only have address of last client from which message have received.

I had also tried storing the addresses of all sockets who connect to server atleast once , so that we can use sendto(), But as all client are running on same machine, That &from seems to be same.

What are alternatives available ? Where am I going wrong ? I simply want to broadcast a message to all client running on local machine. ?

mkkhedawat
  • 1,687
  • 2
  • 17
  • 34
  • I'm confused. Do you mean `multicast` instead of `broadcast`? – Sourav Ghosh Feb 02 '15 at 11:23
  • I want to send message to all clients at the same time. – mkkhedawat Feb 02 '15 at 11:26
  • check the `struct sockaddr_in` structure. There is something called _port number_ [`sin_port`]. – Sourav Ghosh Feb 02 '15 at 11:27
  • That would be same for connection, right ? As there is only one server. – mkkhedawat Feb 02 '15 at 11:28
  • So? your problem is in server side, am I right? Please add some more information and localize your problem in hand. – Sourav Ghosh Feb 02 '15 at 11:30
  • @Manish You can do that with 2 methods, IP broadcast or IP multicast, where you send to a special IP address - that the clients also need be prepared to receive from. IP broadcast can only be done on the same IP subnet(LAN), IP multicast can go beyond a single IP subnet(but requires special configuration of your routers to do so). You need to first chose one of those 2 methods. Or you perform the broadcast yourself in code, but calling sendto() in a loop for each of your clients. – nos Feb 02 '15 at 11:33
  • @nos , thanks for your time. They all are on same subnet. on the same machine to be more correct. When you say , special IP , could you please elaborate. Also as I told , I can't use sendto() in loop as every client have same address . – mkkhedawat Feb 02 '15 at 11:37
  • @SouravGhosh I guess , you misunderstood my question. I am not receiving any error. I just don't know how to proceed for my idea. – mkkhedawat Feb 02 '15 at 11:39
  • You can perhaps find some hints from this question: http://stackoverflow.com/questions/10747107/udp-broadcast-in-c – nos Feb 02 '15 at 11:44
  • But. It is sender and has address. It does not comment anything on receiver side. how will all clients will receive if sent to single address and port. I am more concerned about multiple receivers. – mkkhedawat Feb 02 '15 at 12:15

1 Answers1

0

Sending UDP broadcasts might not be allowed by default by you network stack implementation. You need to enable broadcast in some environments:

int allow_broadcast = 1;
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void*) &allow_broadcast, sizeof(allow_broadcast));

The actual parameter list of setsockopt should be available in the documentation of your OS like 1 or 2.

harper
  • 13,345
  • 8
  • 56
  • 105