Multicast socket - Raspberry Pi Forums


i have been trying figure out how send multicast packets last day. have tried has failed, have attached output 'tcpdump' below along code. wireshark not seem picking multicast packets either. things appear have been sent, nothing picking packet. multicast enabled on router, , using edimax usb wifi dongle raspberry pi. have tried same code multiple ways. code pasted below latest attempt. there 2 sockets being used in program. 1 multicast , other unicast through udp. appreciate given. also, client meant receive these multicast packets written in node.js. assume doesn't matter...

tcpdump:

code: select all

tcpdump: verbose output suppressed, use -v or -vv full protocol decode listening on wlan0, link-type en10mb (ethernet), capture size 262144 bytes 15:21:08.699541 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:13.699929 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:18.700309 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:23.700701 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:28.701091 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:33.701475 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 15:21:38.701848 ip 192.168.1.29.56921 > 234.5.6.7.9090: udp, length 27 
c++ code:

code: select all

struct multicastsocket { 	int localfd; 	int ttl = 60; 	struct in_addr localinterface; 	struct sockaddr_in groupsock; 	char loopback; 	char loop = 0; 	std::string localip; };  void multicastthread(multicastsocket ms) { 	int status = 0; 	 	memset(&ms.groupsock, 0, sizeof(struct sockaddr_in)); 	memset(&ms.localinterface, 0, sizeof(struct in_addr)); 	 	ms.localfd = socket(af_inet, sock_dgram, 0); 	 	if (ms.localfd < 0) 	{ 		perror("error creating socket"); 		exit(1); 	} 	 	ms.groupsock.sin_family = af_inet; 	ms.groupsock.sin_port = htons(0); 	ms.groupsock.sin_addr.s_addr = inet_addr("192.168.1.29"); 	status = bind(ms.localfd, (struct sockaddr *)&ms.groupsock, sizeof(struct sockaddr_in)); 	 	if (status < 0) 	{ 		perror("error binding socket interface"); 		exit(1); 	} 	 	ms.localinterface.s_addr = inaddr_any; 	 	status = setsockopt(ms.localfd, ipproto_ip, ip_multicast_if, &ms.localinterface, sizeof(struct in_addr)); 	 	if (status < 0) 	{ 		perror("setting ip multicast interface failed!"); 		exit(1); 	} 	 	status = setsockopt(ms.localfd, ipproto_ip, ip_multicast_ttl, &ms.ttl, sizeof(char)); 	 	if (status < 0) 	{ 		perror("setting ttl failed"); 		exit(1); 	} 	 	status = setsockopt(ms.localfd, ipproto_ip, ip_multicast_loop, &ms.loop, sizeof(char)); 	 	if (status < 0) 	{ 		perror("setting loopback failed"); 		exit(1); 	} 	 	ms.groupsock.sin_family = af_inet; 	ms.groupsock.sin_addr.s_addr = inet_addr("234.5.6.7"); 	ms.groupsock.sin_port = htons(9090); 	 	int socklen = sizeof(struct sockaddr_in); 	 	json j; 	j["dongleip"] = ms.localip; 	std::string s = j.dump(); 	 	while (true) 	{ 		int sent = sendto(ms.localfd, s.c_str(), s.length(), 0, (struct sockaddr *)&ms.groupsock, socklen); 		 		if (sent < 0) 		{ 			perror("sending datagram message error\n"); 			std::cout << "error: " << errno << std::endl; 			exit(1); 		} 		else 		{ 			printf("sending datagram message...ok\n"); 			printf("sent: %d\n", sent); 		} 		std::this_thread::sleep_for(std::chrono::seconds(5)); 	} } 

i able solve issue using multicast address: 239.255.255.250. once address used, things started working immediately. figured should post in case else has similar issues.


raspberrypi



Comments