Gmane
Favicon
From: zeng国锴 <cougarzeng <at> hotmail.com>
Subject: about IP Multicast
Newsgroups: gmane.linux.drivers.madwifi.user
Date: 2008-08-04 23:45:14 GMT (22 weeks, 3 days, 8 hours and 58 minutes ago)

Hi everyone,

I got a problem when using madwifi

I use a pair of sender - receiver program on two laptops for multicasting, compile successfully, but the receiver cannot get the packets.

I wonder whether the madwifi support the IGMP protocol, or simple multicast operation? Or my madwifi version (March 2007) is not latest enought?  Or my  red hat version  not latest enough?  Or something wrong with the programs? Thanks.

by the way, my madwifi version works well under unicast programs.

The following are the programs

sender:
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_LEN 1024

int main(int argc, char *argv[])
{
int sock;
char message_to_send[MAX_LEN];
unsigned int send_len;
char* multicast_ip;
unsigned short multicast_port;
unsigned char multicast_ttl=1;
struct sockaddr_in multicast_addr;

if (argc != 3)
{
fprintf(stderr, "Usage: %s Multicast_IP Multicast_Port\n", argv[0]);
exit(1);
}

multicast_ip = argv[1]; /* arg 1: multicast IP address */
multicast_port = atoi(argv[2]); /* arg 2: multicast port number */

/* create a socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
perror("Socket creation failed");
exit(1);
}

/* set the TTL (time to live/hop count) for the send */
if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*) &multicast_ttl, sizeof(multicast_ttl))) < 0)
{
perror("setsockopt() failed");
exit(1);
}

memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = inet_addr(multicast_ip);
multicast_addr.sin_port = htons(multicast_port);

printf("Type the message below (Press Enter to send, ctrl-C to quit):\n");

memset(message_to_send, 0, sizeof(message_to_send));

while (fgets(message_to_send, MAX_LEN, stdin))
{
send_len = strlen(message_to_send);

if ((sendto(sock, message_to_send, send_len, 0,
(struct sockaddr *) &multicast_addr,
sizeof(multicast_addr))) != send_len)
{
perror("Error in number of bytes");
exit(1);
}

memset(message_to_send, 0, sizeof(message_to_send));
}

close(sock);

exit(0);
}


receiver:


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_LEN 1024

int main(int argc, char *argv[])
{
int sock;
int flag_on = 1;
struct sockaddr_in multicast_addr;
char message_received[MAX_LEN+1];
int msgrecv_len;
struct ip_mreq mc_req;
char* multicast_ip;
unsigned short multicast_port;
struct sockaddr_in from_addr;
unsigned int from_len;

if (argc != 3)
{
fprintf(stderr, "Usage: %s Multicast_IP Multicast_Port\n", argv[0]);
exit(1);
}

multicast_ip = argv[1]; /* arg 1: multicast ip address */
multicast_port = atoi(argv[2]); /* arg 2: multicast port number */

if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
{
perror("socket() failed");
exit(1);
}

/* set reuse port to on to allow multiple binds per host */
if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag_on,
sizeof(flag_on))) < 0)
{
perror("setsockopt() failed");
exit(1);
}

/* construct a multicast address structure */
memset(&multicast_addr, 0, sizeof(multicast_addr));
multicast_addr.sin_family = AF_INET;
multicast_addr.sin_addr.s_addr = htonl(INADDR_ANY);
multicast_addr.sin_port = htons(multicast_port);

/* bind to multicast address to socket */
if ((bind(sock, (struct sockaddr *) &multicast_addr, sizeof(multicast_addr))) < 0)
{
perror("bind() failed");
exit(1);
}

/* construct an IGMP join request structure */
mc_req.imr_multiaddr.s_addr = inet_addr(multicast_ip);
mc_req.imr_interface.s_addr = htonl(INADDR_ANY);

/* send an ADD MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
{
perror("setsockopt() failed");
exit(1);
}

while(1)
{
memset(message_received, 0, sizeof(message_received));
from_len = sizeof(from_addr);
memset(&from_addr, 0, from_len);

/* block waiting to receive a packet */
if ((msgrecv_len = recvfrom(sock, message_received, MAX_LEN, 0, (struct sockaddr*)&from_addr, &from_len)) < 0)
{
perror("recvfrom() failed");
break;
}

printf("Received %d bytes from %s: ", msgrecv_len,
inet_ntoa(from_addr.sin_addr));
printf("%s", message_received);
}

/* send a DROP MEMBERSHIP message via setsockopt */
if ((setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (void*) &mc_req, sizeof(mc_req))) < 0)
{
perror("setsockopt() failed");
exit(1);
}
close(sock);
}


Best wishes,

Guokai


各位明星最近怎么样?人气榜为您奉上最新动态! 现在就看看!
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Madwifi-users mailing list
Madwifi-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/madwifi-users