过去两天一直在努力解决以下问题:如果发送方订阅了多播组,环回的数据包是否会通过接入点返回发送方?即使不是这样,是否可以通过AP强制环回?
另外,为什么环回不能使用以下代码?
char * server_addr_name = "239.255.0.1"; // multicast group
int port_number = 8888;
int enable_loopback = 1;
int udp_socket;
struct message msg; // random message
char buffer[BUFFER_SIZE];
/* create socket */
struct sockaddr_in server_addr, rcv_addr;
socklen_t server_addr_size = sizeof(struct sockaddr_in);
socklen_t rcv_addr_size = sizeof(struct sockaddr_in);
/* initialize socket */
memset(& server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(server_addr_name);
server_addr.sin_port = htons(port_number);
if ((udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
fprintf(stderr, "Error initializing UDP socket.\n");
exit(EXIT_FAILURE);
}
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(server_addr_name);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(udp_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, & mreq, sizeof(mreq)) < 0) {
fprintf(stderr, "Error on setting multicast membership on socket.\n");
exit(EXIT_FAILURE);
}
unsigned char do_enable = (unsigned char) enable_loopback;
if (setsockopt(udp_socket, IPPROTO_IP, IP_MULTICAST_LOOP,
& do_enable, sizeof(do_enable)) < 0) {
fprintf(stderr, "Error on setting multicast loopback on socket.\n");
exit(EXIT_FAILURE);
}
int read_size = 0;
while (1) {
/* send the packet */
if (sendto(udp_socket, &msg, sizeof(struct message), 0,
(struct sockaddr *) & server_addr, server_addr_size) == -1) {
fprintf(stderr, "Error on sending UDP packet.\n");
}
else
printf("Sent my message.\n");
/* get response from the server/multicast address */
read_size = recvfrom(udp_socket, buffer, BUFFER_SIZE, 0,
(struct sockaddr *) & rcv_addr, & rcv_addr_size);
if (read_size < 1)
break;
else
printf("Got my packet!\n");
}
任何帮助表示赞赏。