Hi
Actually, I was a little motivated to write a little multicast client-server application
myself. The server sends a string once a second (XSleep[1]) to a multicast address.
I've set the TTL to 3 to enable small hops, if the routers support multicasting.
The clients join the multicast group to receive the string. The code does work
as well under Windows as Linux - just a few lines (headers) have to be modified.
This is for educational purposes onlyI tried to be as close to the "common
notation" as possible to simplify comparison with other source codes out there.
The server:
Code:#include <stdio.h> #include <stdlib.h> #include <winsock.h> #include "XSleep.h" int main(){ WSADATA wsda; WSAStartup(0x0101,&wsda); // winsock version number int sockfd; struct sockaddr_in multicast_sockaddr; char multicast_ip[16] = "224.000.111.222"; unsigned short multicast_port = 12345; unsigned char multicast_ttl = 3; char probe[17] = "Hello World! 000"; unsigned int probe_length = strlen(probe); int counter=0; // get a socket file descriptor and set the socket option to multicast sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); setsockopt(sockfd,IPPROTO_IP,IP_MULTICAST_TTL, (const char *)&multicast_ttl, sizeof(multicast_ttl)); // initialise the multicast sockaddr memset(&multicast_sockaddr,0,sizeof(multicast_sockaddr)); multicast_sockaddr.sin_family = AF_INET; multicast_sockaddr.sin_addr.s_addr = inet_addr(multicast_ip); multicast_sockaddr.sin_port = htons(multicast_port); // start the server while(1){ sprintf(probe,"Hello World! %.3d",counter%1000); if ((unsigned)sendto(sockfd,probe,probe_length,0, (struct sockaddr *) &multicast_sockaddr,sizeof(multicast_sockaddr))!=probe_length){ printf("sendto failed. Aborting server."); exit(1); } else printf("Sending: \"%s\"\n",probe); XSleep(1000); counter++; } WSACleanup(); closesocket(sockfd); return 0; }
The client:
Do not forget to link the library WSock32.lib. If you want to readCode:#include <stdio.h> #include <stdlib.h> #include <winsock.h> int main(){ WSADATA wsda; WSAStartup(0x0101,&wsda); // winsock version number int sockfd; struct sockaddr_in multicast_sockaddr; char multicast_ip[16] = "224.000.111.222"; unsigned short multicast_port = 12345; struct ip_mreq multicast_request; char probe[17]; unsigned int probe_length; // get a socket file descriptor sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); // initialise the multicast sockaddr memset(&multicast_sockaddr,0,sizeof(multicast_sockaddr)); multicast_sockaddr.sin_family = AF_INET; multicast_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY); multicast_sockaddr.sin_port = htons(multicast_port); // bind to the port bind(sockfd,(struct sockaddr *) &multicast_sockaddr,sizeof(multicast_sockaddr)); // specify and join the multicast group and accept any interface multicast_request.imr_multiaddr.s_addr=inet_addr(multicast_ip); multicast_request.imr_interface.s_addr=htonl(INADDR_ANY); setsockopt(sockfd, IPPROTO_IP,IP_ADD_MEMBERSHIP,(const char *) &multicast_request, sizeof(multicast_request)); while(1){ probe_length=recvfrom(sockfd,probe,17,0,NULL,0); if ( probe_length < 0){ printf("recvfrom failed. Aborting client."); exit(1); } else{ probe[16]=0; printf("Received: \"%s\"\n",probe); } } WSACleanup(); closesocket(sockfd); return 0; }
more about multicasting, there is a pretty good introduction[2]
available. I would suggest to play with different settings (pim,...),
in particular with heavy load traffic. (Guus' link).
Cheers
[1] http://www.codeguru.com/Cpp/misc/misc/article.php/c211/
[2] http://userpage.fu-berlin.de/~tangua...ticast_rou.htm




I tried to be as close to the "common
Reply With Quote