This is the mail archive of the
ecos-discuss@sourceware.org
mailing list for the eCos project.
Re: limited broadcast IP
- From: Andrew Lunn <andrew at lunn dot ch>
- To: paape at Hi-Techniques dot com
- Cc: ecos-discuss at sources dot redhat dot com
- Date: Thu, 23 Feb 2006 18:30:09 +0100
- Subject: Re: [ECOS] limited broadcast IP
- References: <C426C1EEF0F1BE469844723A96F73CD9034ECA@XSV1>
On Thu, Feb 23, 2006 at 11:17:32AM -0600, paape@Hi-Techniques.com wrote:
> Andrew,
> You've found about a quarter of a page more than me.
TCP/IP Illustrated, Volume 1: The protocols. W. Richard Stevens.
Section 12.2, page 171.
> I have a foot in both
> the UNIX and Windows camps (and I'm not very comfortable with the Windows
> side). Our product incorporates systems running both Windows and eCos.
> Right or wrong, the Windows part seems to send the type of packet that I'm
> describing, by default. I have an eCos thread that receives it and responds
> directly to the sender. So far so good. Now, as part of this product, I
> will have other eCos systems that also need to query info from this first
> (eCos) system. It however, does not appear to receive the subnet
> broadcast.
Try this code. It should receive broadcast packets sent to port 4567.
int sock, err;
struct sockaddr_in addr;
char buffer[64];
wlink_msg_t msg;
int one = 1;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1) {
perror("socket");
exit(-1);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(4567);
addr.sin_addr.s_addr = INADDR_ANY;
err= setsockopt(sock, SOL_SOCKET, SO_BROADCAST,&one, sizeof(one));
if (err == -1) {
perror("setsockopt(SO_BROADCAST,1)");
exit(-1);
}
err= setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,&one, sizeof(one));
if (err == -1) {
perror("setsockopt(SO_REUSEADDR,1)");
exit(-1);
}
err = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
if (err == -1) {
perror("bind()");
exit(-1);
}
while(1) {
err = recvfrom(sock, buffer, sizeof(buffer),0, NULL, 0);
if (err < 0) {
perror("read()ing from socket");
exit(-1);
}
memcpy((char *)&msg.dst, &buffer[2],32);
I've only tested this on Linux....
Andrew
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss