Loading ...
Sorry, an error occurred while loading the content.

Multicast Receiver

Expand Messages
  • jed111
    I am learning network programming. My current I idea is to create an application to benchmark multicast performance. I am having trouble enabling a socket to
    Message 1 of 1 , Mar 12, 2008
    • 0 Attachment
      I am learning network programming.
      My current I idea is to create an application to benchmark multicast
      performance.
      I am having trouble enabling a socket to receive multicast traffic.
      Below is a sample of my code. I have used hard coded socket option
      constants as I have found no definitive header file.

      When the MC group join executes, setsockopt() returns an unknown
      error.

      Can someone show me the proper sequence to enable a socket to
      receive multicast traffic.

      use strict;
      use Socket;

      my $IPPROTO_IP = getprotobyname ('ip');
      # IPPROTO Level Options
      my $IP_ADD_MEMBERSHIP = 5; # set only
      my $IP_DROP_MEMBERSHIP = 6; # set only
      my $IP_MULTICAST_IF = 2; # get/set
      my $IP_MULTICAST_LOOP = 4; # get/set
      my $IP_MULTICAST_TTL = 3; # get/set
      my $IP_OPTIONS = 1;

      my $SOL_SOCKET = 0xffff;
      # SOCKET Level Options
      my $SO_DEBUG = 1;
      my $SO_ACCEPTCONN = 2;
      my $SO_REUSEADDR = 4;
      my $SO_KEEPALIVE = 8;
      my $SO_DONTROUTE = 16;
      my $SO_BROADCAST = 32;
      my $SO_USELOOPBACK = 64;
      my $SO_LINGER = 128;
      my $SO_OOBINLINE = 256;
      my $SO_SNDBUF = 0x1001;
      my $SO_RCVBUF = 0x1002;
      my $SO_SNDLOWAT = 0x1003;
      my $SO_RCVLOWAT = 0x1004;
      my $SO_SNDTIMEO = 0x1005;
      my $SO_RCVTIMEO = 0x1006;
      my $SO_ERROR = 0x1007;
      my $SO_TYPE = 0x1008;

      my $rxaddr = inet_aton ('192.168.1.101');
      my $mcrxaddr = inet_aton ('239.255.255.123');
      my $transport = getprotobyname ('udp');
      my $rxport = 4001;
      my $ucreceiver = sockaddr_in ($rxport, $rxaddr);
      my $mcreceiver = sockaddr_in ($rxport, $mcrxaddr);

      socket (UDP_SOCK, PF_INET, SOCK_DGRAM, $transport) or die "udp_s2:
      socket creation failed: $!\n";
      setsockopt (UDP_SOCK, $IPPROTO_IP, $SO_REUSEADDR, pack ("l", 0)) or
      die ("Reuse");
      setsockopt (UDP_SOCK, $SOL_SOCKET, $SO_RCVBUF, pack ("L", 233000))
      or die ("RCVBUFF");
      setsockopt (UDP_SOCK, $IPPROTO_IP, $IP_MULTICAST_LOOP, pack ("l",
      0)) or die ("Loop");

      bind (UDP_SOCK, $ucreceiver) or die "upd_s2: bind to address failed:
      $!\n";
      setsockopt (UDP_SOCK, $IPPROTO_IP, $IP_ADD_MEMBERSHIP, $rxaddr) or
      die "ADD MC GROUP: $!\n";

      close (UDP_SOCK);
    Your message has been successfully submitted and would be delivered to recipients shortly.