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));
}
}
}
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));
}
}
}