Advertisement
  Home arrow C Source arrow Multicast Client Example Saturday, 31 July 2010 
Main Menu
 Home
 News
 FAQ
 C Source
 The Web Links
 Contact Us
 Administrator

Login Form
Username

Password

Remember me
Forgotten your password?
No account yet? Create one

Statistics
OS: Windows
PHP: 5.2.7-dev
MySQL: 4.0.15-nt
Time: 23:12
Members: 1126
Hits: 844043
News: 13
WebLinks: 6

Online Users
No Users Online

Newsflash
Host Extensions for IP Multicasting [RFC1112] specifies the extensions
required of a host implementation of the Internet Protocol (IP) to
support multicasting.  The multicast addressess are in the range
224.0.0.0 through 239.255.255.255. Current addresses are listed below. www.iana.org/assignments/multicast-addresses

Multicast Client Example   PDF  Print  E-mail 
Contributed by Bilbo Baggins  
Tuesday, 01 June 2004
IP Multicast source for client

/*  multirec.c - see also diewitherror.c and multisnd.c */

/* compile  $ cc -o multirec.exe multirec.c diewitherror.c */

 

/*                   program      multicast ip  port */

/* useage example $ ./multirec.exe 224.0.22.1 9210  */

 

/* tested with cygwin gcc win32- linux gcc -sun */

 

#include <stdio.h>      /* for printf() and fprintf() */

#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */

#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */

#include <stdlib.h>     /* for atoi() and exit() */

#include <string.h>     /* for memset() */

#include <unistd.h>     /* for close() */

 

 

#include <time.h> /* modified for timestamps */

 

 

 

#define MAXRECVSTRING 255  /* Longest string to receive */

 

void DieWithError(char *errorMessage);  /* External error handling function */

 

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

{

    int sock;                         /* Socket */

    struct sockaddr_in multicastAddr; /* Multicast Address */

    char *multicastIP;                /* IP Multicast Address */

    unsigned short multicastPort;     /* Port */

    char recvString[MAXRECVSTRING+1]; /* Buffer for received string */

    int recvStringLen;                /* Length of received string */

    struct ip_mreq multicastRequest;  /* Multicast address join structure */

 

    if (argc != 3)    /* Test for correct number of arguments */

    {

        fprintf(stderr,"Usage: %s <Multicast IP> <Multicast Port>\n", argv[0]);

        exit(1);

    }

 

    multicastIP = argv[1];        /* First arg: Multicast IP address (dotted quad) */

    multicastPort = atoi(argv[2]);/* Second arg: Multicast port */

 

    /* Create a best-effort datagram socket using UDP */

    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)

        DieWithError("socket() failed");

 

    /* Construct bind structure */

    memset(&multicastAddr, 0, sizeof(multicastAddr));   /* Zero out structure */

    multicastAddr.sin_family = AF_INET;                 /* Internet address family */

    multicastAddr.sin_addr.s_addr = htonl(INADDR_ANY);  /* Any incoming interface */

    multicastAddr.sin_port = htons(multicastPort);      /* Multicast port */

 

    /* Bind to the multicast port */

    if (bind(sock, (struct sockaddr *) &multicastAddr, sizeof(multicastAddr)) < 0)

        DieWithError("bind() failed");

 

    /* Specify the multicast group */

    multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastIP);

    /* Accept multicast from any interface */

    multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);

    /* Join the multicast address */

    if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void *) &multicastRequest,

          sizeof(multicastRequest)) < 0)

        DieWithError("setsockopt() failed");

 

    /* Receive a single datagram from the server */

    /* Start loop here */

 

int c;

int x1 = 1000000; /* initialize loop variables */

int x2 = 0;

 

while (x2 < x1)

{

/* x1++ */

/* uncomment the line above to have a limit */

if ((recvStringLen = recvfrom(sock, recvString, MAXRECVSTRING, 0, NULL, 0)) < 0)

        DieWithError("recvfrom() failed");

 

    recvString[recvStringLen] = '\0';

 

 

 

time_t tim=time(NULL);  /* get time stamp to print with recieved data */

             char *s=ctime(&tim);

             s[strlen(s)-1]=0;        // remove \n

         /*    printf("it is %s now.\n", s); */

     

    printf("Time Received: %s : %s\n", s, recvString);    /* Print the received string */

 

}

    close(sock);

    exit(0);

 

} /* main */

 

   

 

 

 

Last Updated ( Tuesday, 29 June 2004 )

Browser Prefs
Add to Favorites
Make Home Page

Newsfeeds
Internet:Business News
Sat, 31 Jul 2010 22:47
With Apple and Google bearing down, the early mover in enterprise smartphones has its quarterly numbers fall short.
RIM Earnings Show Heat From iPhone, Android
The technology world's most quotable quips and comments from around the Web.
Say What? The Week's Top Five IT Quotes
The software and server giant posted a profit of $3 billion for the latest quarter.
Oracle Rides Sun to Big 4Q Earnings
BBC: Technology
Sat, 31 Jul 2010 23:02
Owners of mobile phones are being asked to test the security of their network to see if enough is being done to stop eavesdropping.
Call to check on mobile security
Twitter, the social networking site which allows users to say something in up to 140 characters, sees its 20 billionth message sent.
Twitter passes 20 billionth tweet
Newsbeat's had an exclusive look at new training being given to UK soldiers at the Royal School of Artillery in Wiltshire.
UK troops use iPad app for fire mission training

Most Read
Multicast Client Example
Multicast Server Example
Welcome to IP Multicast Standards Initiative
diewitherror.c Multicast Client & Server
What ipmulticast.com will provide

: Home :: News :: FAQ :: C Source :: The Web Links :: Contact Us :: Administrator :
powered by world multicast ietf rmt wg ietf rmt wg