lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 27, Adrian Sietsma wrote:
> In short : has anyone tried to run 2 applications which join the same
> multicast group and port on windows or other OS ?
>
> Adrian

Attached is C code for two programs mlisten.c & mcast.c

I was able to run 3 instances of mlisten.c, that joined the same multicast IP 
listening on the same UDP port number, all on the same linux host.  Then I 
started mcast.c on the same linux host which sent a message every second to 
that multicast IP & port.

-- 
Robert G. Jakabosky
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define HELLO_PORT "45565"
#define HELLO_GROUP "228.0.0.41"

main(int argc, char *argv[])
{
	struct sockaddr_in addr;
	int fd, cnt;
	struct ip_mreq mreq;
	char *message="Hello, World!";
	char *dest_ip=HELLO_GROUP;
	char *dest_port=HELLO_PORT;

	if(argc > 1) {
		dest_ip = argv[1];
	}
	if(argc > 2) {
		dest_port = argv[2];
	}

	/* create what looks like an ordinary UDP socket */
	if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
		perror("socket");
		exit(1);
	}

	/* set up source address */
	memset(&addr,0,sizeof(addr));
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
	//addr.sin_addr.s_addr=inet_addr("10.10.0.108");
	addr.sin_port=htons(atoi(dest_port)+1);
   
	/* bind to receive address */
	if(bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
		perror("bind");
		exit(1);
	}

	/* set up destination address */
	memset(&addr,0,sizeof(addr));
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=inet_addr(dest_ip);
	addr.sin_port=htons(atoi(dest_port));
 
	/* now just sendto() our destination! */
	while(1) {
		if(sendto(fd,message,strlen(message)+1,0,(struct sockaddr *) &addr, sizeof(addr)) < 0) {
			perror("sendto");
			exit(1);
		}
		sleep(1);
	}
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

#define HELLO_PORT 45565
#define HELLO_GROUP "228.0.0.41"
#define MSGBUFSIZE 8192
#define VECTOR_SIZE 10

#define IOV_BUF(x,y) ((char *)x[y].iov_base)

main(int argc, char *argv[])
{
	struct sockaddr_in addr;
//	struct sockaddr_in6 raddr;
	char raddr[MSGBUFSIZE];
	int fd, nbytes,raddrlen;
	struct ip_mreq mreq;
	char msg[MSGBUFSIZE];
	struct iovec riov[VECTOR_SIZE];
	char *p;
	int one = 1, i = 0;

	if(argc > 1) {
		snprintf(msg, MSGBUFSIZE, "listener %s", argv[1]);
	} else {
		strcpy(msg, "listener");
	}

	/* create what looks like an ordinary UDP socket */
	if((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
		perror("socket");
		exit(1);
	}

	/* Allow sharing of the socket. */
	if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
		perror("setsocketopt");
		exit(1);
	}

	/* set up receive address */
	memset(&addr,0,sizeof(addr));
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=htonl(INADDR_ANY); /* N.B.: differs from sender */
	addr.sin_port=htons(HELLO_PORT);
   
	/* bind to receive address */
	if(bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
		perror("bind");
		exit(1);
	}

	/* use setsockopt() to request that the kernel join a multicast group */
	mreq.imr_multiaddr.s_addr=inet_addr(HELLO_GROUP);
	mreq.imr_interface.s_addr=htonl(INADDR_ANY);
	//mreq.imr_interface.s_addr=inet_addr("10.10.0.108");
	if(setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
		perror("setsockopt");
		exit(1);
	}

	/* now just enter a read-print loop */
	nbytes = 0;
	//sleep(40);
	while(1) {
		raddrlen = MSGBUFSIZE;
		raddrlen = 0;
		//if((nbytes=recvfrom(fd, msg, MSGBUFSIZE, 0, (struct sockaddr *)&raddr, &raddrlen)) < 0) {
		if((nbytes=recv(fd, msg, MSGBUFSIZE, 0)) < 0) {
			if(errno != EAGAIN) {
				perror("recvfrom");
				exit(1);
			} else {
				printf("recvfrom: error(%d) %s\n", errno, strerror(errno));
				continue;
			}
		}
		printf("recvfrom: nbytes=%d, raddrlen=%d: ", nbytes, raddrlen);
		//if(nbytes == 38) printf("%s", (char *)(msg+16));
		for(i = 0; i < raddrlen; i++) {
			printf("%2.2hhX", raddr[i]);
			if((i & 0x3) == 0 && i > 0) printf(" ");
		}
		printf(" : %s", (char *)(msg));
		/*
		for(i = 0; i < nbytes; i++) {
			printf("%2.2hhX", msg[i]);
			if((i & 0x3) == 0 && i > 0) printf(" ");
		}
		*/
		printf("\n");
		fflush(stdout);
		//sleep(20);
		//usleep(1000);
		// reset send header.
	}
}

udp        0      0 0.0.0.0:45565           0.0.0.0:*                           29253/mlisten       
udp        0      0 0.0.0.0:45565           0.0.0.0:*                           29234/mlisten       
udp        0      0 0.0.0.0:45565           0.0.0.0:*                           29205/mlisten       
udp        0      0 0.0.0.0:45566           0.0.0.0:*                           29182/mcast