Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > UDP Multicast Question

Reply
Thread Tools

UDP Multicast Question

 
 
neilsolent
Guest
Posts: n/a
 
      01-21-2009
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));
}
}
}
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-21-2009
neilsolent wrote:
> 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));
> }
> }
> }



--

Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
neilsolent
Guest
Posts: n/a
 
      01-22-2009
Sorry, clicked on the wrong group by mistake.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP multicast problem with winsock Gary C C++ 2 05-19-2010 12:18 PM
Python sockets UDP broadcast multicast question?? inorlando Python 3 08-29-2008 05:49 AM
udp multicast server and client skpopu@gmail.com C Programming 3 04-25-2008 07:00 PM
Multicast UDP deamon and VLAN interfaces pietro.cerutti@gmail.com C Programming 7 12-01-2006 05:21 PM
Multicast UDP deamon and VLAN interfaces pietro.cerutti@gmail.com C Programming 0 12-01-2006 08:38 AM



Advertisments