How can I implement multi-cast socket connection using C programming -
there problem packets drops. doing socket connection udp packets, think there may problem in multi-cast socket connection. please me — code proper or not?
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> int main(int argc, char *argv[]) { struct sockaddr_in si_me, si_other; struct ip_mreq mreq; socklen_t slen = sizeof(si_other); int s = -1; if ((s = socket(af_inet, sock_dgram, ipproto_udp)) == -1) { printf("error creating socket\n"); } int yes = 1; if (setsockopt(s, sol_socket, so_reuseaddr, &yes, sizeof(int)) == -1) { printf("setsockopt");exit(1); } int val = 1; if (setsockopt(s, sol_socket, so_reuseport, (const char *) &val, sizeof(val)) < 0) { } memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = af_inet; si_me.sin_port = htons(42000); si_me.sin_addr.s_addr = htonl(inaddr_any); //bind socket port if (bind(s, (struct sockaddr*) &si_me, sizeof(si_me)) == -1) { printf("error binding port\n"); } //multicast socket mreq.imr_multiaddr.s_addr = inet_addr("232.0.1.1"); mreq.imr_interface.s_addr = htonl(inaddr_any); if (setsockopt(s, ipproto_ip, ip_add_membership, &mreq, sizeof(mreq)) < 0) { printf("setsockopt mreq"); exit(1); } int recv_len; while(true) { if ((recv_len = (int) recvfrom(s, data, buflen, 0,(struct sockaddr *) &si_other, &slen)) == -1) { printf("error receiving pakcets\n"); } } }
Comments
Post a Comment