Code comments

#define MAXBUFSIZE 65536 // Max UDP Packet size is 64 Kbyte

Sets MAXBUFSIZE to 65536 = 2^16, because of UDP header definitions, see UDP header


sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if ( sock < 0 )
  perror("Error creating socket"), exit(0);

Create a UDP/IP socket


saddr.sin_family = PF_INET;
saddr.sin_port = htons(4096); // listen on port 4096
saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind socket to any interface

Set parameters for bind:


status = bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in));
if ( status < 0 )
  perror("Error binding socket to interface"), exit(0);

Bind the socket to specified interface and port


imreq.imr_multiaddr.s_addr = inet_addr("226.0.0.1");
imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface
// JOIN multicast group on default interface
status = setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, 
(const void *)&imreq, sizeof(struct ip_mreq));


socklen = sizeof(struct sockaddr_in);
// receive packet from socket
status = recvfrom(sock, buffer, MAXBUFSIZE, 0, 
(struct sockaddr *)&saddr, &socklen);


// shutdown socket
shutdown(sock, 2);
// close socket
close(sock);