This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Problem reading broadcast IP
- From: "Emmanuel Coullien" <coullien dot emmanuel at gmail dot com>
- To: ecos-discuss at ecos dot sourceware dot org
- Date: Fri, 18 Jan 2008 10:00:56 +0100
- Subject: [ECOS] Problem reading broadcast IP
Hi,
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;
srv_recept.sin_addr.s_addr=inet_addr("192.168.1.3");
if ((srecept = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("socket");
return;
}
// positionner le socket en mode BROADCAST
if (setsockopt(srecept, SOL_SOCKET, SO_BROADCAST, &FlgON, sizeof(FlgON)))
{
perror(" : setsockopt");
return;
}
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);
else
{ Bufrecept[n_read]='\0';
printf(" Reception OK (%s)\n",Bufrecept);
}
}
else
{
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("192.168.1.3");"
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("192.168.1.3") 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("192.168.1.3");. 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