Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Network broadcast programming

Hi,

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.
0
Gene Klamerus
Asked:
Gene Klamerus
1 Solution
 
jcaldwelCommented:
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");
  exit(1);
}
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));
0
 
jmcgOwnerCommented:
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now