Network broadcast programming

Posted on 2003-03-05
Medium Priority
Last Modified: 2008-03-03

We're creating an application to do distributed measurement of application performance that runs on workstations and that we can deploy on our network.

We'd like for these "watchdog" PCs to announce themselves to the nearest data consolidation server.

So, what we'd like to do is have them do some sort of a broadcast to find the servers that exist and are available to receive the data they're collecting.

How would one write a "broadcast" network request (like ARP or a DHCP client request) to announce themselves.  We would know the "port" to use, but not the IP address to direct the message to (it should be anything that responds).

Any example code that does this would be even better.
Question by:klamerus
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

jcaldwel earned 400 total points
ID: 8073762
For a UNIX Based Server (Juicy parts only):

int s = socket(AF_INET, SOCK_DGRAM, 0);

on = 1;
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {
  syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");
bind(s, (struct sockaddr *) &sin, sizeof (sin));

int cc, whod, len = sizeof (from);

cc = recvfrom(s, buff, sizeof (buff), 0,
            (struct sockaddr *)&from, &len);

For a UNIX Based Client:

struct ifreq ifr;
struct in_addr broadcast;
ret = ioctl(fd,SIOCGIFBRDADDR,&ifr);
 if (ret==-1) {
  perror("Error getting broadcast"); exit (1);
memcpy(&sin,&ifr.ifr_broadaddr,sizeof(struct sockaddr_in));
 broadcast = sin.sin_addr.s_addr;

int cc;
int s;

s = socket(AF_INET, SOCK_DGRAM, htons(ETH_P_ARP));

cc = sendto(s, buff, sizeof( buff ), 0, (struct sockaddr*)&broadcast, sizeof(broadcast));
LVL 20

Expert Comment

ID: 10089262
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by jcaldwel.


EE Cleanup Volunteer

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses
Course of the Month12 days, 18 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question