Client/server Multicast API

N

nazgulero

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...

// Code du serveur
#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.111"
#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("Problemes lors de l'ouverture de la socket ");
exit(1);
}

memset(&servaddr, 0, len_serv);
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;

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


inet_aton(GROUP, &(imr.imr_multiaddr));
imr.imr_interface.s_addr = INADDR_ANY;

if(setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr))
< 0)
{
perror("Problemes lors de la demande de JOIN ");
exit(1);
}

if(recvfrom(sockfd, message_recu, TAILLE, 0, (struct sockaddr
*)&servaddr, &len_serv) < 0)
{
perror("Problemes lors de la lecture d'une donnee ");
exit(1);
}
printf("%s\n", message_recu);
close(sockfd);

}


// Code du client

#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.111"
#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("Problemes lors de l'ouverture de la socket ");
exit(1);
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
inet_aton(GROUP, &(servaddr.sin_addr));

if(sendto(sockfd, message, strlen(message), 0, (struct sockaddr
*)&servaddr, sizeof(servaddr)) < 0)
{
perror("Problemes lors de l'envoi du message ");
exit(1);
}

}

Your help is greatly appreciated.

Regards,

GP
 
A

Artie Gold

nazgulero said:
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

First of all, the code you posted is C, not C++ code.
Second, it makes use of -- and totally depends on -- POSIXisms that are
neither part of standard C nor C++.

Please try you're off topic here.

HTH,
--ag

[snip]
 
N

nazgulero

Hello Artie,

I think need to get a hold of the `C++ for Dummies´ book first before
posting...my apologies for the OT, and thanks to pointing me to the
right group...

GP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top