[ECOS] Problem reading broadcast IP

Emmanuel Coullien coullien.emmanuel@gmail.com
Fri Jan 18 09:01:00 GMT 2008


I' am trying to get broadcast frame from Ethernet with the following code :

void Test_system_UDP2(void)
    int n_read;
    struct timeval tv;
    int srecept;
    struct sockaddr_in srv_recept;
    int FrameErrCountSocket1=0;
    int DataErrCountSocket1=0;
	int RecvError=0;
    int ErrSendSocket=0;
	int CountSocket1=0;
    int i;
    unsigned char j;
    int StartCycle =1;
    /* for select on socket */
    int ret;
    fd_set file_set;

	int FlgON = 1;   // Utiliser pour activer des Options dans SetSockOpt
    /*** Init de la socket de reception ***/
    bzero(&srv_recept, sizeof(srv_recept));
    srv_recept.sin_family = AF_INET;
    srv_recept.sin_port = htons(PORT_TO_SEND);
//    srv_recept.sin_addr.s_addr=INADDR_ANY;

    if ((srecept = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    // positionner le socket en mode BROADCAST
    if (setsockopt(srecept, SOL_SOCKET, SO_BROADCAST, &FlgON, sizeof(FlgON)))
        perror(" : setsockopt");

    if (bind(srecept, (struct sockaddr *)&srv_recept, sizeof(srv_recept)) < 0)
        printf("Erreur :bind");

    tv.tv_sec = TIMEOUT_RECV;
    tv.tv_usec = 0;
    while (1)
            FD_ZERO (&file_set);
            FD_SET (srecept, &file_set);
            ret = select (srecept+1, &file_set, NULL, NULL, &tv);
            if (ret >0)
                /* we have data in the socket */
                n_read = recvfrom(srecept, Bufrecept, MAXBUF, 0,NULL, NULL);
                if (n_read < 0)
                     printf("Erreur lecture socket : n_read +%d\n",n_read);
                {   Bufrecept[n_read]='\0';
                    printf(" Reception OK (%s)\n",Bufrecept);
                printf("Erreur Timeout : ret +%d\n",ret);

If I use "srv_recept.sin_addr.s_addr=INADDR_ANY;", then it works fine,
I receive my broadcast frame.
But if I use  "srv_recept.sin_addr.s_addr=inet_addr("");"
even with  "setsockopt(srecept, SOL_SOCKET, SO_BROADCAST, &FlgON,
sizeof(FlgON)" ,
then on eCos I can't receive any broadcast.
I need to use inet_addr("") to select this interface
because I have 3 Ethernet interface and I don't want to receive from
the other interface.

I tried on a PC and I haven't any problem with
srv_recept.sin_addr.s_addr=inet_addr("");. I received the
broadcast frame.

Does someone know why I didn't received the broadcast frame in that
case on eCos ?
Is there something I missed or an other socket option to set ?

Emmanuel Coullien

Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list