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


// 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.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);


}


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

Jack Klein

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>

[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 if you use Linux, or
generically
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top