玩组播,收集部分代码,调测通过。
mrecv.c
#include#include #include <string.h> #include #include #include #include #include in.h> #include if.h> #define MCAST_GRP "224.1.1.1" #define MCAST_PORT 5566 #define MAXBUF 10240 int main() { int n, r, sockfd = -1; struct sockaddr_in srv, cli; struct ip_mreq mreq; char buf[MAXBUF]; memset( &srv, 0, sizeof(struct sockaddr_in) ); memset( &cli, 0, sizeof(struct sockaddr_in) ); memset( &mreq, 0, sizeof(struct ip_mreq) ); srv.sin_family = AF_INET; srv.sin_port = htons(MCAST_PORT); if (inet_aton(MCAST_GRP, &srv.sin_addr) < 0) { fprintf(stderr, "inet_aton"); return -1; } if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { fprintf(stderr, "socket"); return -1; } if (bind(sockfd, (struct sockaddr *)&srv, sizeof(srv)) < 0) { fprintf(stderr, "bind"); return -1; } if (inet_aton(MCAST_GRP, &mreq.imr_multiaddr) < 0) { fprintf(stderr, "inet_aton"); return -1; } { struct ifreq ifreq; strncpy(ifreq.ifr_name, "eth0", IFNAMSIZ); if (ioctl(sockfd, SIOCGIFADDR, &ifreq) < 0) { fprintf(stderr, "ioctl"); return -1; } memcpy(&mreq.imr_interface, &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr, sizeof(struct in_addr)); } if (setsockopt(sockfd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { fprintf(stderr, "setsockopt"); return -1; } while(1) { if ((n = recv(sockfd, buf, MAXBUF, MSG_DONTWAIT)) > 0) { buf[n] = 0; //fprintf(stdout, "message: %s", buf); printf("received %s\n", buf); } else if (0 == n) break; } return 0; }
mrecv.py
#!/usr/bin/env python import socket import struct MCAST_GRP = '224.1.1.1' MCAST_PORT = 5566 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) sock.bind((MCAST_GRP, MCAST_PORT)) mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) while True: print 'recv', sock.recv(10240)
msend.c
#include#include #include <string.h> #include #include #include #include #include in.h> #include if.h> #define MCAST_GRP "224.1.1.1" #define MCAST_PORT 5566 #define MAXBUF 10240 int main() { int n, r, sockfd = -1; struct sockaddr_in srv, cli; struct ip_mreq mreq; char buf[MAXBUF]; memset( &srv, 0, sizeof(struct sockaddr_in) ); memset( &cli, 0, sizeof(struct sockaddr_in) ); memset( &mreq, 0, sizeof(struct ip_mreq) ); srv.sin_family = AF_INET; srv.sin_port = htons(MCAST_PORT); if (inet_aton(MCAST_GRP, &srv.sin_addr) < 0) { fprintf(stderr, "inet_aton"); return -1; } if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { fprintf(stderr, "socket"); return -1; } while(1) { sendto(sockfd, "Hello World", strlen("Hello World"), 0, (struct sockaddr*)&srv,sizeof(struct sockaddr_in)); sleep(1); } }
msend.py
#!/usr/bin/env python import socket MCAST_GRP = '224.1.1.1' MCAST_PORT = 5566 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) while True: import time, random _content = str(random.random()) print "send", _content sock.sendto(_content, (MCAST_GRP, MCAST_PORT)) time.sleep(3)