#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>

#ifdef LYNXOS
#include <bsd/in.h>
#include <arpa/inet.h>
#endif

main(int argc,char ** argv )
{
#ifdef LYNXOS
  const unsigned char TTL=224;
#else
  const int TTL=224;
#endif
  char buffer[4096];
  const int v_off=0, v_on=1;
  struct sockaddr_in to;
  struct ip_mreq multiaddr;
  int m;
  long addr;
  struct in_addr in;
  
  char c_addr[16];
  int sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef LYNXOS
   addr=inet_addr( argv[1]);
   in.s_addr = addr;
   multiaddr.imr_multiaddr.s_addr =htonl(addr);
#else
  inet_pton(AF_INET, argv[1], &addr);
  multiaddr.imr_multiaddr.s_addr =addr;
#endif

  multiaddr.imr_interface.s_addr = htonl(INADDR_ANY);
  if(setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &multiaddr, 
	     sizeof(struct ip_mreq)) < 0) perror("IP_ADD_MEMBERSHIP: ");
  if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &TTL, sizeof(TTL)) < 0)
     perror("IP_MULTICAST_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(in));
#else
  inet_ntop(AF_INET, &to.sin_addr.s_addr, &c_addr, 16);
#endif
  for (m =0; m<100; m++)
    {
      buffer[0]=m;
      if(sendto(sock, buffer, 100, 0, 
	     (const struct sockaddr *)&to, sizeof(to))
	== -1)
	perror("couldn't send");
      
      printf("sending to %s\n",c_addr);
      sleep(1);
    }
  close(sock);
}

