UDP Multicast Question

N

neilsolent

Hi

Please see my code below (shortened - error code checking etc removed
for brevity).
This is a program that listens on UDP port 5002, and joins multicast
group "236.185.113.0". It listens for 2-byte messages (the app will do
something with these later, obviously).

The code works - it intercepts the multicasted 2-byte messages. My
issue is - I want the code to ONLY listen to the multicasts - at the
moment it picks up unicast and broadcast UDP messages on port 5002 as
well. Is there a modification I can make to either not listen for non-
multicast messages, or filter them out somehow ?

thanks,
Neil

CODE:



int mcastRecvSocket = socket(AF_INET, SOCK_DGRAM, 0);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5002);
addr.sin_addr.s_addr = INADDR_ANY;

bind(mcastRecvSocket, (sockaddr*) &addr, sizeof(addr));

ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("236.185.113.0");
mreq.imr_interface.s_addr = htons(INADDR_ANY);

setsockopt(mcastRecvSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq));

char udpMsg[4];
struct sockaddr_in peer;
int peerLength = sizeof(peer);
fd_set read_set;
struct timeval timeout;
int recv_return;

for (;;)
{
FD_ZERO(&read_set);
FD_SET(mcastRecvSocket, &read_set);

timeout.tv_sec = 10;
timeout.tv_usec = 0;

select(max(msgPipe, mcastRecvSocket) + 1, &read_set, NULL, NULL,
&timeout);

if (FD_ISSET(mcastRecvSocket, &read_set))
{
recv_return = recvfrom(mcastRecvSocket, udpMsg, 4, 0, (struct
sockaddr*) &peer, (socklen_t*) &peerLength);

if (recv_return == 2)
{
printf("recvfrom() read message %u from IP address %s\n", *
((unsigned short*) udpMsg), inet_ntoa(peer.sin_addr));
}
}
}
 
K

Knute Johnson

neilsolent said:
Hi

Please see my code below (shortened - error code checking etc removed
for brevity).
This is a program that listens on UDP port 5002, and joins multicast
group "236.185.113.0". It listens for 2-byte messages (the app will do
something with these later, obviously).

The code works - it intercepts the multicasted 2-byte messages. My
issue is - I want the code to ONLY listen to the multicasts - at the
moment it picks up unicast and broadcast UDP messages on port 5002 as
well. Is there a modification I can make to either not listen for non-
multicast messages, or filter them out somehow ?

thanks,
Neil

No. A UDP is a UDP. You'll have to filter them based on something else.
CODE:



int mcastRecvSocket = socket(AF_INET, SOCK_DGRAM, 0);

sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(5002);
addr.sin_addr.s_addr = INADDR_ANY;

bind(mcastRecvSocket, (sockaddr*) &addr, sizeof(addr));

ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("236.185.113.0");
mreq.imr_interface.s_addr = htons(INADDR_ANY);

setsockopt(mcastRecvSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
sizeof(mreq));

char udpMsg[4];
struct sockaddr_in peer;
int peerLength = sizeof(peer);
fd_set read_set;
struct timeval timeout;
int recv_return;

for (;;)
{
FD_ZERO(&read_set);
FD_SET(mcastRecvSocket, &read_set);

timeout.tv_sec = 10;
timeout.tv_usec = 0;

select(max(msgPipe, mcastRecvSocket) + 1, &read_set, NULL, NULL,
&timeout);

if (FD_ISSET(mcastRecvSocket, &read_set))
{
recv_return = recvfrom(mcastRecvSocket, udpMsg, 4, 0, (struct
sockaddr*) &peer, (socklen_t*) &peerLength);

if (recv_return == 2)
{
printf("recvfrom() read message %u from IP address %s\n", *
((unsigned short*) udpMsg), inet_ntoa(peer.sin_addr));
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top