UDP Multicast Question

Discussion in 'Java' started by neilsolent, Jan 21, 2009.

  1. neilsolent

    neilsolent Guest

    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));
    }
    }
    }
     
    neilsolent, Jan 21, 2009
    #1
    1. Advertising

  2. 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
     
    Knute Johnson, Jan 21, 2009
    #2
    1. Advertising

  3. neilsolent

    neilsolent Guest

    Sorry, clicked on the wrong group by mistake.
     
    neilsolent, Jan 22, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    700
    CBFalconer
    Dec 1, 2006
  2. Replies:
    0
    Views:
    440
  3. udp multicast server and client

    , Apr 25, 2008, in forum: C Programming
    Replies:
    3
    Views:
    848
    Sri Harsha Dandibhotla
    Apr 25, 2008
  4. inorlando
    Replies:
    3
    Views:
    1,339
    castironpi
    Aug 29, 2008
  5. Gary C
    Replies:
    2
    Views:
    2,324
    Jorgen Grahn
    May 19, 2010
Loading...

Share This Page