How do I get a broadcast UDP datagram to work?
UDP datagrams to directed IP addresses work.
I started from udpecho TIRTOS example project.
NDK is controlled through udpecho.syscfg.
CCS 12.5.0.0000067
C:\ti\simplelink_msp432e4_sdk_4_20_00_12
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
How do I get a broadcast UDP datagram to work?
UDP datagrams to directed IP addresses work.
I started from udpecho TIRTOS example project.
NDK is controlled through udpecho.syscfg.
CCS 12.5.0.0000067
C:\ti\simplelink_msp432e4_sdk_4_20_00_12
Hi,
I'm currently travelling. I will respond to your post next week.
As far as I understand (i.e, this seemed to work), to send a multicast, you just send to the correct multicast address.
e.g.
const char * MULTICAST = "224.0.1.129"; struct sockaddr_in clientAddr; socklen_t addrlen = sizeof(clientAddr); clientAddr.sin_family = AF_INET; clientAddr.sin_port = htons(EVENT_PORT); clientAddr.sin_addr.s_addr = inet_addr(MULTICAST); // The required IP address int bytesSent = sendto(LocalFD, pBuffer, bytesToSend, 0, (struct sockaddr *)&clientAddr, addrlen); if (bytesSent < 0) { Display_printf(display, 0, 0, "Error: sendto failed."); }
To receive from a multicast, you must register with the multicast first:
/// A temporary struct used for setting multicast address. typedef struct my_ip_mreq { struct in_addr imr_multiaddr; /**< IP Address of the group */ struct in_addr imr_interface; /**< IP Address of the interface */ }my_ip_mreq; struct my_ip_mreq mreq; mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST); mreq.imr_interface.s_addr = htonl(INADDR_ANY); int res = setsockopt(LocalFD, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); Display_printf(display, 0, 0, "Join 1st multicast on PTP port returned %d", res); /* Set the SO_REUSEPORT option. */ int optval = 1; res = setsockopt(LocalFD, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval)); Display_printf(display, 0, 0, "Set SO_REUSEPORT on PTP port returned %d", res);
That's good, because I realised you actually asked about Broadcast, not Multicast!
For Broadcast, I believe that you just send to an address with all the local address bits set to 1. e.g. If Your network is 192.168.0.n, you broadcast to 192.168.0.255 - There is a good Wikipedia article on it
For broadcast, I needed:
int allow_broadcast = 1;
status = setsockopt(server, SOL_SOCKET, SO_BROADCAST, (void*) &allow_broadcast, sizeof(allow_broadcast));
Otherwise, sendto() fails.