why select() waits until timeout at first call even if data is available for multicast socket

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật