#define MULTICAST

#include <stdio.h>
#include <sys/types.h>
#ifdef LINUX
#include <linux/socket.h>
#else
#include <socket.h> 
#endif
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>

main(int argc,char ** argv )
{
  const int TTL=224;
  char buffer[4096];
  const int v_off=0, v_on=1;
  struct sockaddr_in to;
  struct ip_mreq multiaddr;
  int m;
  long addr;
  char c_addr[16];
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef LYNXOS
  addr=inet_addr( argv[1]);
  multiaddr.imr_multiaddr.s_addr =htonl(addr);
#else
  inet_pton(AF_INET, argv[1], &addr);
  printf("address=0x%x\n", addr);
  multiaddr.imr_multiaddr.s_addr =addr;
#endif
  multiaddr.imr_interface.s_addr = htonl(INADDR_ANY);
  setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &multiaddr, 
	     sizeof(multiaddr));
  setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &TTL, sizeof(TTL));
  
  to.sin_family=AF_INET;
#ifdef LYNXOS
  to.sin_addr.s_addr=htonl(addr); 
#else
  to.sin_addr.s_addr=addr;
#endif
  to.sin_port=htons(12345);
#ifdef LYNXOS
 sprintf(c_addr, inet_ntoa(to));
#else
  inet_ntop(AF_INET, &to.sin_addr.s_addr, &c_addr, 16);
#endif
  if (bind(sock, (const struct sockaddr *)&to, sizeof(struct sockaddr_in)) < 0)
  {
    perror("multircv: bind failed: ");
    exit(1);
  } 


  for (; ; )
    {
      int chars;
      int len =  sizeof(to);
     
      if((chars= recvfrom(sock, buffer, 100, 0, 
			 (const struct sockaddr *)&to, &len))== -1)
	{
	  perror("rcvfrom failed");
	  exit(1);
	}
      printf("received %d chars %d\n",chars, buffer[0]);
    }
  close(sock);
}
