Created
November 21, 2022 14:57
-
-
Save soda92/00b1c8e14a3500cdd899b5bca93a04df to your computer and use it in GitHub Desktop.
Sending Udp multicast on Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern "C" { | |
#include <winsock2.h> | |
} | |
// https://en.wikipedia.org/wiki/Multicast_address | |
int main() | |
{ | |
auto wsaData = WSAData {}; | |
WSAStartup(MAKEWORD(2, 0), &wsaData); | |
auto sock = socket(AF_INET, SOCK_DGRAM, 0); | |
auto addr = sockaddr_in { | |
.sin_family = AF_INET, | |
.sin_port = htons(12345) | |
}; | |
addr.sin_addr.S_un.S_addr = inet_addr("224.0.75.80"); | |
auto yes = 1; | |
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, | |
(char*)&yes, sizeof(yes)); | |
auto message = "HELLO"; | |
sendto(sock, message, strlen(message), 0, | |
(struct sockaddr*)&addr, sizeof(addr)); | |
closesocket(sock); | |
WSACleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment