I try to use multicast sockets on my linux.
I copied that simple server prog:
/* server.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sstream>
#include <iostream>
static int port = 1234;
int main (void) {
int socket_descriptor;
struct sockaddr_in address;
socket_descriptor = socket (AF_INET, SOCK_DGRAM, 0);
if (socket_descriptor == -1)
{
perror ("socket()");
exit (EXIT_FAILURE);
}
memset (&address, 0, sizeof (address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr ("224.0.0.1");
address.sin_port = htons (port);
printf ("Server is ready ...n");
/* Broadcasting start */
int cnt = 0;
while (1)
{
std::ostringstream os;
os << "Some text " << cnt ;
cnt++;
std::cout << strlen(os.str().c_str()) << std::endl;
char content[256];
strcpy( content, os.str().c_str());
std::cout << content << std::endl;
if (
sendto( socket_descriptor, (void*)content, strlen(content)+1, 0,
(struct sockaddr *) &address,
sizeof (address)) < 0
)
{
perror ("sendto()");
exit (EXIT_FAILURE);
}
sleep (1);
}
return EXIT_SUCCESS;
}
and this client part:
/* client.c */
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
/* Adresse für multicast IP */
static const char *host_name = "224.0.0.1";
static int port = 1234;
static struct ip_mreq command;
static int setup_multicast_socket (void)
{
int loop = 1;
int socket_descriptor;
struct sockaddr_in sin;
memset (&sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (INADDR_ANY);
sin.sin_port = htons (port);
if ( ( socket_descriptor = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
{
perror ("socket()");
exit (EXIT_FAILURE);
}
/* enable reuse */
loop = 1;
if (setsockopt ( socket_descriptor,
SOL_SOCKET,
SO_REUSEADDR,
&loop, sizeof (loop)) < 0)
{
perror ("setsockopt:SO_REUSEADDR");
exit (EXIT_FAILURE);
}
if( bind( socket_descriptor, (struct sockaddr *)&sin, sizeof(sin)) < 0 )
{
perror ("bind");
exit (EXIT_FAILURE);
}
/* enable broadcast on system */
loop = 1;
if (setsockopt ( socket_descriptor,
IPPROTO_IP,
IP_MULTICAST_LOOP,
&loop, sizeof (loop)) < 0)
{
perror ("setsockopt:IP_MULTICAST_LOOP");
exit (EXIT_FAILURE);
}
/* Join the broadcast group: */
command.imr_multiaddr.s_addr = inet_addr ("224.0.0.1");
command.imr_interface.s_addr = htonl (INADDR_ANY);
if (command.imr_multiaddr.s_addr == -1)
{
perror ("224.0.0.1 ist keine Multicast-Adressen");
exit (EXIT_FAILURE);
}
if (setsockopt ( socket_descriptor,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
&command, sizeof (command)) < 0)
{
perror ("setsockopt:IP_ADD_MEMBERSHIP");
}
return socket_descriptor;
}
int main (void)
{
int iter = 0;
socklen_t sin_len;
char message[256];
int socket;
struct sockaddr_in sin;
// select
int retval;
fd_set rfds;
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
socket = setup_multicast_socket ();
FD_ZERO(&rfds);
FD_SET(socket, &rfds);
/* receive messages */
while(1)
{
select( 1, &rfds, NULL, NULL, &tv); // first time waits 2 seconds, after first call it terminates if data is available...
//select( 1, &rfds, NULL, NULL, NULL ); // endless timeout, never continues ??? Why
sin_len = sizeof (sin);
if (recvfrom( socket, message, 256, 0, (struct sockaddr *) &sin, &sin_len)==-1)
{
perror ("recvfrom");
}
printf ("Answer #%-2d from server: %sn", iter, message);
iter++;
}
/* Multicast-Socket aus der Gruppe entfernen */
if (setsockopt ( socket,
IPPROTO_IP,
IP_DROP_MEMBERSHIP,
&command, sizeof (command)) < 0 )
{
perror ("setsockopt:IP_DROP_MEMBERSHIP");
}
close (socket);
return EXIT_SUCCESS;
}
I expect, that the select call returns immediately if the socket becomes readable. But it did not at the first call! If I use it with the timeout value NULL
it simply hangs endless. If I use a timeout, it runs into the timeout, reads from the socket and in the next loop everything seems to work as expected. What I am doing wrong?