Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 18 Mar 2004 13:36:12 +0100
From:      "Fuhua Yin" <fuhuayin@hotmail.com>
To:        freebsd-net@freebsd.org
Cc:        yin@helios.iihe.ac.be
Subject:   Multicast IPv6, socket,
Message-ID:  <BAY16-F67kxJ5zHQE7m00017300@hotmail.com>

next in thread | raw e-mail | index | archive | help
Dear ,

I am trying to work on IPv6 multicast. I just wrote a small programme to 
test but get stuck somewhere. I wonder if someone could be of help.


Many thanks in advance!!
fuhua

This is a client to send data from keyboard to group ff3e::2222/1234.
-------
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv){

        int mysock;
        struct sockaddr_in6 servaddr6, cliaddr6;
        struct ipv6_mreq mreq6;

        int loop=0;
        int hops=127;
        int ifindex;

        int servlen;
        int n;
        char sendline[1024], recvline[1025];

        mysock=socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        ifindex=if_nametoindex("eth0");

        bzero(&servaddr6, sizeof(servaddr6));
        servaddr6.sin6_family=AF_INET6;
        servaddr6.sin6_port=htons(1234);
        inet_pton(AF_INET6, "ff3e::2222", &servaddr6.sin6_addr);

        if(!IN6_IS_ADDR_MULTICAST(&servaddr6.sin6_addr)){
int main(int argc, char **argv){

        int mysock;
        struct sockaddr_in6 servaddr6, cliaddr6;
        struct ipv6_mreq mreq6;

        int loop=0;
        int hops=127;
        int ifindex;

        int servlen;
        int servlen;
        int n;
        char sendline[1024], recvline[1025];

        mysock=socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        ifindex=if_nametoindex("eth0");

        bzero(&servaddr6, sizeof(servaddr6));
        servaddr6.sin6_family=AF_INET6;
        servaddr6.sin6_port=htons(1234);
        inet_pton(AF_INET6, "ff3e::2222", &servaddr6.sin6_addr);

        if(!IN6_IS_ADDR_MULTICAST(&servaddr6.sin6_addr)){

        printf(" is not multicast ip6 address \n");
        exit(1);
        }


        bzero(&cliaddr6, sizeof(cliaddr6));
        cliaddr6.sin6_family=AF_INET6;
        cliaddr6.sin6_port=htons(0);
        inet_pton(AF_INET6, "2001:6a8:1080:102:203:47ff:fe69:ab93", 
&cliaddr6.sin6_addr);

        bind(mysock, (struct sockaddr*)& cliaddr6,sizeof(cliaddr6));

        setsockopt(mysock, IPPROTO_IPV6, IPV6_MULTICAST_IF, 
&ifindex,sizeof(ifindex));
        setsockopt(mysock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, 
sizeof(loop));
        setsockopt(mysock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, 
sizeof(hops));

        while (fgets(sendline, 1024,stdin)!=NULL){

             sendto(mysock, sendline, strlen(sendline),0, (struct 
sockaddr*)&servaddr6,sizeof(servaddr6));
             printf("fuhua check\n");
             fputs(sendline,stdout);

        }
return 0;

}
-----------------------

This is the server to receive the message and print on screen.

--
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv){

int mysock;
struct sockaddr_in6 myaddr, cliaddr;
char msgbuf[1024];
int clilen;
int msglen;
   mysock=socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
   memset(&myaddr,0, sizeof(myaddr));
   myaddr.sin6_family=AF_INET6;
   myaddr.sin6_port=htons(1234);
   inet_pton(AF_INET6,"2001:6a8:1080:102::d",&myaddr.sin6_addr);

   printf("ssss \n");
   mysock=socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
   bind(mysock, (struct sockaddr*) &myaddr, sizeof(myaddr));
for ( ; ; )
     {

        printf("fuhua yin----ssss \n");
        clilen=sizeof(cliaddr);
        msglen=recvfrom(mysock, msgbuf, 1024, 0, (struct sockaddr*)&cliaddr, 
&clilen);

        printf("how many bytes I receive from client %d",msglen);
        sendto(mysock, msgbuf,msglen, 0, (struct sockaddr*)&cliaddr, 
clilen);

      }

return 0;

_________________________________________________________________




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?BAY16-F67kxJ5zHQE7m00017300>