Client/Server Multicast API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nazgulero

    #1

    Client/Server Multicast API

    Hello all,


    I am fairly new to C, and I am trying to write a client/server API
    for multicast. I have come across the script below, but for some
    reason, the server does not get any of the multicast traffic from the
    client, so I am afraid that somewhere there might be a mistake in the
    script. Would anybody care to have a quick check ? Never mind the
    French comments, I ´borrowed´ the script from a French website...


    // Server code
    #include <stdio.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>


    #define GROUP "239.137.194.11 1"
    #define PORT 55555
    #define TAILLE 512


    int main()
    {
    int sockfd;
    struct sockaddr_in servaddr;
    struct ip_mreq imr;
    int len_serv;
    char message_recu[TAILLE];


    memset(message_ recu, 0, TAILLE);


    len_serv = sizeof(servaddr );


    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
    perror("Problem es lors de l'ouverture de la socket ");
    exit(1);



    }


    memset(&servadd r, 0, len_serv);
    servaddr.sin_fa mily = AF_INET;
    servaddr.sin_po rt = htons(PORT);
    servaddr.sin_ad dr.s_addr = INADDR_ANY;

    if(bind(sockfd, (struct sockaddr *)&servaddr, len_serv) < 0)
    {
    perror("Problem es lors de l'association de la socket a un port ");
    exit(1);



    }


    inet_aton(GROUP , &(imr.imr_multi addr));
    imr.imr_interfa ce.s_addr = INADDR_ANY;

    if(setsockopt(s ockfd, IPPROTO_IP, IP_ADD_MEMBERSH IP, &imr, sizeof(imr))

    < 0)
    {
    perror("Problem es lors de la demande de JOIN ");
    exit(1);



    }


    if(recvfrom(soc kfd, message_recu, TAILLE, 0, (struct sockaddr
    *)&servaddr, &len_serv) < 0)
    {
    perror("Problem es lors de la lecture d'une donnee ");
    exit(1);

    }


    printf("%s\n", message_recu);
    close(sockfd);


    }


    // Client Code

    #include <stdio.h>
    #include <string.h>
    #include <netinet/in.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>


    #define GROUP "239.137.194.11 1"
    #define PORT 55555
    #define TAILLE 512


    int main()
    {
    int sockfd;
    struct sockaddr_in servaddr;
    char message[TAILLE];


    memset(message, 0, TAILLE);
    sprintf(message , "Coucou");


    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
    perror("Problem es lors de l'ouverture de la socket ");
    exit(1);



    }


    memset(&servadd r, 0, sizeof(servaddr ));
    servaddr.sin_fa mily = AF_INET;
    servaddr.sin_po rt = htons(PORT);
    inet_aton(GROUP , &(servaddr.sin_ addr));

    if(sendto(sockf d, message, strlen(message) , 0, (struct sockaddr
    *)&servaddr, sizeof(servaddr )) < 0)
    {
    perror("Problem es lors de l'envoi du message ");
    exit(1);



    }
    }


    Your help is greatly appreciated.

    Regards,


    GP

  • Jack Klein

    #2
    Re: Client/Server Multicast API

    On 16 May 2005 03:29:26 -0700, "nazgulero" <georg.pauwen@w anadoo.nl>
    wrote in comp.lang.c:
    [color=blue]
    > Hello all,
    >
    >
    > I am fairly new to C, and I am trying to write a client/server API
    > for multicast. I have come across the script below, but for some
    > reason, the server does not get any of the multicast traffic from the
    > client, so I am afraid that somewhere there might be a mistake in the
    > script. Would anybody care to have a quick check ? Never mind the
    > French comments, I ´borrowed´ the script from a French website...
    >
    >
    > // Server code
    > #include <stdio.h>
    > #include <string.h>
    > #include <netinet/in.h>
    > #include <unistd.h>
    > #include <sys/socket.h>
    > #include <signal.h>
    > #include <sys/types.h>
    > #include <sys/stat.h>
    > #include <fcntl.h>[/color]

    [snip]

    Sorry, wrong newsgroup. Three of the headers above are part of the C
    language, the other six are non-standard extensions provided by your
    particular compiler/OS combination and off-topic here. Standard C
    does not provide any built-in support for any sort of networking.

    You need to ask about this in a platform specific group, for your
    specific UNIX variant.

    Perhaps news:comp.os.li nux.development .apps if you use Linux, or
    generically news:comp.unix. programmer.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c++-faq-lite/
    alt.comp.lang.l earn.c-c++

    Comment

    Working...