linux - multicast loopback in c -
i trying test multicast loopback clients join group , server multicast messgaes client in group constrained in 127.0.0.1 !!!!!
refering : http://www.tenouk.com/module41c.html , source :
the server :
struct in_addr localinterface; struct sockaddr_in groupsock; int sd; char databuf[1024] = "multicast test message lol!"; int datalen = sizeof(databuf); int main (int argc, char *argv[ ]) { /* create datagram socket on send. */ sd = socket(af_inet, sock_dgram, 0); if(sd < 0) { perror("opening datagram socket error"); exit(1); } else printf("opening datagram socket...ok.\n"); /* initialize group sockaddr structure */ /* group address of 225.1.1.1 , port 5555. */ memset((char *) &groupsock, 0, sizeof(groupsock)); groupsock.sin_family = af_inet; groupsock.sin_addr.s_addr = inet_addr("224.0.0.0"); groupsock.sin_port = htons(4321); /* disable loopback not receive own datagrams. * { * char loopch = 0; * if(setsockopt(sd, ipproto_ip, ip_multicast_loop, (char *)&loopch, sizeof(loopch)) < 0) * { * perror("setting ip_multicast_loop error"); * close(sd); * exit(1); * } * else * printf("disabling loopback...ok.\n"); * } * */ /* set local interface outbound multicast datagrams. */ /* ip address specified must associated local, */ /* multicast capable interface. */ localinterface.s_addr = inet_addr("127.0.0.1"); if(setsockopt(sd, ipproto_ip, ip_multicast_if, (char *)&localinterface, sizeof(localinterface)) < 0) { perror("setting local interface error"); exit(1); } else printf("setting local interface...ok\n"); /* send message multicast group specified the*/ /* groupsock sockaddr structure. */ /*int datalen = 1024;*/ while( 1 ){ usleep( 1000 ) ; if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupsock, sizeof(groupsock)) < 0) { perror("sending datagram message error"); } else printf("sending datagram message...ok\n"); } /* try re-read socket if loopback not disable * if(read(sd, databuf, datalen) < 0) * { * perror("reading datagram message error\n"); * close(sd); * exit(1); * } * else * { * printf("reading datagram message client...ok\n"); * printf("the message is: %s\n", databuf); * } * */ return 0; }
and client source :
struct sockaddr_in localsock; struct ip_mreq group; int sd; int datalen; char databuf[1024]; int main(int argc, char *argv[]) { /* create datagram socket on receive. */ sd = socket(af_inet, sock_dgram, 0); if(sd < 0) { perror("opening datagram socket error"); exit(1); } else printf("opening datagram socket....ok.\n"); /* enable so_reuseaddr allow multiple instances of */ /* application receive copies of multicast datagrams. */ { int reuse = 1; if(setsockopt(sd, sol_socket, so_reuseaddr, (char *)&reuse, sizeof(reuse)) < 0) { perror("setting so_reuseaddr error"); close(sd); exit(1); } else printf("setting so_reuseaddr...ok.\n"); } /* bind proper port number ip address */ /* specified inaddr_any. */ memset((char *) &localsock, 0, sizeof(localsock)); localsock.sin_family = af_inet; localsock.sin_port = htons(4321); localsock.sin_addr.s_addr = inaddr_any; if(bind(sd, (struct sockaddr*)&localsock, sizeof(localsock))) { perror("binding datagram socket error"); close(sd); exit(1); } else printf("binding datagram socket...ok.\n"); /* join multicast group 226.1.1.1 on local 203.106.93.94 */ /* interface. note ip_add_membership option must */ /* called each local interface on multicast */ /* datagrams received. */ group.imr_multiaddr.s_addr = inet_addr("224.0.0.0"); group.imr_interface.s_addr = inet_addr("127.0.0.1"); if(setsockopt(sd, ipproto_ip, ip_add_membership, (char *)&group, sizeof(group)) < 0) { perror("adding multicast group error"); close(sd); exit(1); } else printf("adding multicast group...ok.\n"); /* read socket. */ datalen = sizeof(databuf); while( 1 ){ if(read(sd, databuf, datalen) <= 0) { perror("reading datagram message error"); close(sd); exit(1); } else { printf("reading datagram message...ok.\n"); printf("the message multicast server is: \"%s\"\n", databuf); } } //while return 0; }
after add following root , works fine :
route add -net 224.0.0.0 netmask 240.0.0.0 dev lo ifconfig lo multicast
if have 1 client.exe running , perfect , , if run 2 copies of client.exe , server.exe or client.exe hang , if need more 1 application join group(in sample 224.0.0.0) , should modify source many clients may join muticast gruop , receive packets without troubles ?!
Comments
Post a Comment