Narzędzia użytkownika

Narzędzia witryny


sk2:multicast_example

Różnice

Różnice między wybraną wersją a wersją aktualną.

Odnośnik do tego porównania

Nowa wersja
Poprzednia wersja
sk2:multicast_example [2016/11/21 22:13]
jkonczak utworzono
sk2:multicast_example [2021/12/20 13:49] (aktualna)
jkonczak
Linia 1: Linia 1:
 ==== Przykład użycia UDP multicast ==== ==== Przykład użycia UDP multicast ====
-<code cpp multicast_example>​+<code cpp multicast_example.cpp> 
 +#include <cstdlib>
 #include <​cstdio>​ #include <​cstdio>​
 #include <​csignal>​ #include <​csignal>​
Linia 25: Linia 26:
  ​* ​  - bind to some address and MCASTPORT  ​* ​  - bind to some address and MCASTPORT
  ​* ​  - prepare ip_mreqn structure – fill it with MCASTIP and optionally (but recommended) with local IP and interface id  ​* ​  - prepare ip_mreqn structure – fill it with MCASTIP and optionally (but recommended) with local IP and interface id
- ​* ​  - call setsockopt to set option IP_ADD_MEMBERSHIP ​of the domain ​IPPROTO_IP to the aforementioned structure+ ​* ​  - call setsockopt to set option IP_ADD_MEMBERSHIP ​on level IPPROTO_IP to the aforementioned structure
  ​* ​  - receive datagrams  ​* ​  - receive datagrams
  ​* ​  ​* ​
  * setsockopt of IP_ADD_MEMBERSHIP sends an IGMP message "join group" and tells OS that the program waits for messages for the group.  * setsockopt of IP_ADD_MEMBERSHIP sends an IGMP message "join group" and tells OS that the program waits for messages for the group.
  ​* ​  ​* ​
- * More info at: http://www.tldp.org/​HOWTO/text/​Multicast-HOWTO+ * More info at: https://​tldp.org/​HOWTO/​Multicast-HOWTO-6.html
  */  */
  
Linia 54: Linia 55:
  // create socket  // create socket
  int sockfd = socket(PF_INET,​ SOCK_DGRAM, IPPROTO_UDP);​  int sockfd = socket(PF_INET,​ SOCK_DGRAM, IPPROTO_UDP);​
- if(sockfd==-1) error(1,​errno,​ "sendto");+ if(sockfd==-1) error(1,​errno,​ "socket");
   
  // set SO_REUSEADDR (or only one app will be allowed to receive multicast messages from the groupIP:​port pair)  // set SO_REUSEADDR (or only one app will be allowed to receive multicast messages from the groupIP:​port pair)
Linia 115: Linia 116:
  // close socket AND interrupt all blocked recvfrom ​  // close socket AND interrupt all blocked recvfrom ​
  shutdown(sockfd,​ SHUT_RDWR);  shutdown(sockfd,​ SHUT_RDWR);
 + close(sockfd);​
  // wait for child  // wait for child
  readerThread.join();​  readerThread.join();​
sk2/multicast_example.1479762787.txt.gz · ostatnio zmienione: 2016/11/21 22:13 przez jkonczak