开发者

Multicasting UDP in C

I am programming a multicast environment scenario. The source is sending the content to a multicast address say 239.0.1.1. on the other end a client wants to join this multicast address. The actual working of multicast is that a client who wants to join a multicast group sends a join request to the router it has connected. I have to program the router operation where it receives a multicast join request from a client, fetch the data from the multicast address and give the content to the customer. I am not able to figure it out how to do this. For a single user its fine. But when there are multiple clients the technique I am using is not correct... please help me out.. A code to join a particular address and send the data to a single client is shown below:

#include <sys/types.h>  /* for type definitions */
#include <sys/socket.h> /* for socket API calls */
#include <netinet/in.h> /* for address structs */
#include <arpa/inet.h>  /* for sockaddr_in */
#include <stdio.h>      /* for printf() and fprintf() */
#include <stdlib.h>     /* for atoi() */
#include <string.h>     /* for strlen() */
#include <unistd.h>     /* for close() */

#define MAX_LEN  4074   /* maximum receive string size */
#define MIN_PORT 1024   /* minimum port allowed */
#define MAX_PORT 65535  /* maximum port allowed */


int main(int argc, char *argv[]) 
{

  int sock;                     /* socket descriptor */
  int flag_on = 1;              /* socket option flag */
  struct sockaddr_in mc_addr;   /* socket address structure */
  char recv_str[MAX_LEN+1];     /* buffer to receive string */
  int recv_len;                 /* length of string received */
  struct ip_mreq mc_req;        /* multicast request structure */
  char* mc_addr_str;            /* multicast IP address */
  unsigned short mc_port;       /* multicast port */
  //unsigned short mc_port1=1400;      /*multicast port */
  struct sockaddr_in from_addr; /* packet source */
  unsigned int from_len;        /* source addr length */
  // to send the data to the client

     int sockfd, newsockfd, portno;
     socklen_t clilen;
    struct sockaddr_in serv_addr, cli_addr;
     int n;
   int cnt;
    int no=4096,i;
  char *buffer[no];

  int dest_sock;
  struct sockaddr_in dest_addr;
  char * dest_address="192.168.1.4";
  unsigned int dest_port=5000;

  /* validate number of arguments */
  if (argc != 3)
   {
    fprintf(stderr, 
            "Usage: %s <Multicast IP> <Multicast Port>\n", 
            argv[0]);
    exit(1);
   }

  mc_addr_str = argv[1];      /* arg 1: mlticast ip address */
  mc_port = atoi(argv[2]);    /* arg 2: multicast port number */

  /* validate the port range */
  if ((mc_port < MIN_PORT) || (mc_port > MAX_PORT)) {
    fprintf(stderr, "Invalid port number argument %d.\n",
            mc_port);
    fprintf(stderr, "Valid range is between %d and %d.\n",
            MIN_PORT, MAX_PORT);
    exit(1);
  }

  /* create socket to join multicast group on */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
    perror("socket() failed");
    exit(1);
  }
  else
   printf("Socket connection successful\n");


  /* set reuse port to on to a开发者_Go百科llow multiple binds per host */
  if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
       sizeof(flag_on))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  }


  /* construct a multicast address structure */
  memset(&mc_addr, 0, sizeof(mc_addr));
  mc_addr.sin_family      = AF_INET;
  mc_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  mc_addr.sin_port        = htons(mc_port);


      /* bind to multicast address to socket */
  if ((bind(sock, (struct sockaddr *) &mc_addr, 
       sizeof(mc_addr))) < 0) {
    perror("bind() failed");
    exit(1);
  }

  /* construct an IGMP join request structure */
  mc_req.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
  mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

  /* send an ADD MEMBERSHIP message via setsockopt */
  if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
       (void*) &mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");
    exit(1);
  }
printf("setsocket successfull\n");

//for sending the data to the client

//creating a socket to the client and waiting for the client to receive the data


  if ((dest_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
   perror("socket() failed");
  exit(1);
  }
  else
   printf("Socket connection successful\n");
  if ((setsockopt(dest_sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
     sizeof(flag_on))) < 0) {
   perror("setsockopt() failed");
   exit(1);
  }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = 5000;//atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    if (bind(sockfd, (struct sockaddr *) &serv_addr,
     //         sizeof(serv_addr)) < 0) 
     //         error("ERROR on binding");
    // listen(sockfd,5);
    // clilen = sizeof(cli_addr);
    // newsockfd = accept(sockfd, 
                 //(struct sockaddr *) &cli_addr, 
     //          // &clilen);
     //if (newsockfd < 0) 
   //  error("ERROR on accept");
/*------------------------------------------------------------------------------------------------------*/

 for(cnt=0;cnt<100;cnt++) {          /* loop forever */

    /* clear the receive buffers & structs */
    memset(recv_str, 0, sizeof(recv_str));
    from_len = sizeof(from_addr);
    memset(&from_addr, 0, from_len);

    /* block waiting to receive a packet */
    //buffer[cnt]=recvfrom(sock, recv_str, MAX_LEN, 0,(struct sockaddr*)&from_addr, &from_len);
   // no++;
    if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0,(struct sockaddr*)&from_addr, &from_len)) < 0) 
    //if(strlen(buffer[cnt])<0) 
    {
            perror("recvfrom() failed");
            exit(1);
        }

    /* output received string */
   // printf("Received %d bytes from %s: ", recv_len, inet_ntoa(from_addr.sin_addr));
    printf("\n");
    //printf("%s\n",recv_str);
        //printf("\n");
    //buffer[i]=recv_str;
    //printf("%s\n",buffer[i]);
    //printf("Enter the writing mode\n");
    n = write(newsockfd,recv_str,sizeof(recv_str));
    i++;
    //printf("%d\n",n);
       if (n < 0) 
    {
    error("ERROR writing to socket");
    exit(1);
    }
        i++;

     //return 0; 

  }
   close(newsockfd);
   close(sockfd);
  /* send a DROP MEMBERSHIP message via setsockopt */
  if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, 
       (void*) &mc_req, sizeof(mc_req))) < 0) {
    perror("setsockopt() failed");

    exit(1);
  }
  close(sock);

}


It sounds like you're trying to implement a multicast router, like xorp.

The source for xorp is fairly dense, but igmpproxy has functionality that also sounds similar to what you're after, without a heap of other functionality that you don't need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜