Hi,
I was trying to run the following:
on -n SOME-HOST-NAME [-d] ./mcast-sample
(the source of mcast-sample is below)
but it outputs:
From host:MY_HOST_IP port:3456, Message number: 1
From host:MY_HOST_IP port:3456, Message number: 2
From host:MY_HOST_IP port:3456, Message number: 3
…
but i expected that will be
From host:SOME-HOST-NAME_IP port:3456, Message number: 1
…
it seems – the results of that test the same as I run it on MY machine !!!
WHY?
Thanks a lot,
vasa
<><><><><><><><><><><>
// File mcast-sample.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#define BUFSIZE 1024
#define TTL_VALUE 2
#define TEST_ADDR “234.5.6.7”
#define TEST_PORT 3456
#define LOOPMAX 20
#define TRUE 1
#define FALSE 0
void xerr(char* s){
perror(s);
exit(-1);
}
int main(int argc,char** argv)
{
struct sockaddr_in stLocal, stTo, stFrom;
char achIn[BUFSIZE];
char achOut[] = "Message number: ";
int s, i;
struct ip_mreq stMreq;
int iTmp;
char ttl,loop;
/* get a datagram socket */
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s < 0)
xerr (“socket() failed”);
/* avoid EADDRINUSE error on bind() */
iTmp = TRUE;
if (0>setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&iTmp,
sizeof(iTmp)))
xerr (“setsockopt() SO_REUSEADDR failed”);
if(0 > setsockopt(s, SOL_SOCKET, SO_REUSEPORT, (char *)&iTmp,
sizeof(iTmp)))
xerr (“setsockopt() SO_REUSEADDR failed”);
/* name the socket /
stLocal.sin_family = AF_INET;
stLocal.sin_addr.s_addr = htonl(INADDR_ANY);
stLocal.sin_port = htons(TEST_PORT);
if(0 > bind(s, (struct sockaddr) &stLocal, sizeof(stLocal)))
xerr (“bind() failed”);
/* join the multicast group. */
stMreq.imr_multiaddr.s_addr = inet_addr(TEST_ADDR);
stMreq.imr_interface.s_addr = INADDR_ANY;
if (0 > setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq,
sizeof(stMreq)))
xerr (“setsockopt() IP_ADD_MEMBERSHIP”);
/* set TTL to traverse up to multiple routers /
ttl = TTL_VALUE;
if (0 > setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (char)&ttl,
sizeof(ttl)))
xerr (“setsockopt() IP_MULTICAST_TTL”);
/* disable loopback */
loop = FALSE;
if (0 > setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loop,
sizeof(loop)))
xerr (“setsockopt() IP_MULTICAST_LOOP”);
loop = TRUE;
if (0 > setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loop,
sizeof(loop)))
xerr (“setsockopt() IP_MULTICAST_LOOP”);
/* assign our destination address */
stTo.sin_family = AF_INET;
stTo.sin_addr.s_addr = inet_addr(TEST_ADDR);
stTo.sin_port = htons(TEST_PORT);
printf (“Now sending to (and receiving from) multicast group: %s\n”,
TEST_ADDR);
// for (i=0;i<LOOPMAX;i++) {
for (i=0;;i++) {
static int iCounter = 1;
int addr_size = sizeof(struct sockaddr_in);
/* send to the multicast address /
itoa(iCounter++, &achOut[16], 10);
if(0 > sendto(s, achOut, sizeof(achOut), 0, (struct sockaddr)&stTo,
addr_size))
xerr (“sendto() failed”);
if(0 > recvfrom(s, achIn, BUFSIZE, 0, (struct sockaddr*)&stFrom,
&addr_size))
xerr (“sendto()”);
printf(“From host:%s port:%d, %s\n”,
inet_ntoa(stFrom.sin_addr), ntohs(stFrom.sin_port), achIn);
sleep (2);
} /* end for(; */
return 0;
} /* end main() */