?
Solved

upd broadcast in C

Posted on 2003-03-12
6
Medium Priority
?
322 Views
Last Modified: 2010-05-18
Hi

I want to send out udp broadcast to the whole subnet in the simplest way possible. I'm not familiar with socket.h to figure out what to do. Can someone give me some pointer? Sorry for the small amount of point, that's all i have for now. I'll add more once i get more. Thanx
0
Comment
Question by:1ce
[X]
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
  • 4
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
jcaldwel earned 120 total points
ID: 8123231
A slightly simplified example:

#include <unistd.h>
#include <sys/socket.h>

int s, on;
struct sockaddr dst;
struct ifreq *ifr;
char sendbuff[ 1024 ];

/* Create And Set Up Socket */
s = socket(AF_INET, SOCK_DGRAM, 0);

on = 1;
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof on)
== -1) {
  perror(LOG_ERR, "setsockopt SO_BROADCAST");
  return;
}

/* Get The Broadcast address */
if (ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0) {
  perror( "ioctl" );
  return;
}

memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof ifr->ifr_broadaddr);


/* Send the message */

sendto(s, sendbuf, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));


0
 
LVL 3

Author Comment

by:1ce
ID: 8123866
Hi
when i compile this give an error:

if (ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0) {

sizeof applied to an incomplete types

i'm on freebsd 4.7
0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8123900
Try changing:

memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof ifr->ifr_broadaddr);
to:
memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof(struct sockaddr_in));

and

if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof on)

To
if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int))


Add add an "#include <sys/ioctl.h>" to the top
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Author Comment

by:1ce
ID: 8124920
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv){
   int s, on;
   struct sockaddr dst;
   struct ifreq *ifr;
   char sendbuff[ 1024 ]="balbalbal";
   //char *sendaddr="10.10.255.255";
   /* Create And Set Up Socket */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   on = 1;

   if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int)) == -1) {
      printf("setsockopt SO_BROADCAST");
      return 0;
   }

   if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
      printf( "ioctl error" );
      return;
   }
   memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
   sendto(s, sendbuff, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));

}

----------------------------------------------

It still giving the same error msg when compile. =(

>>if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
socket_udp_bd.c:21: sizeof applied to an incomplete type

>>memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
socket_udp_bd.c:25: dereferencing pointer to incomplete type
socket_udp_bd.c:25: sizeof applied to an incomplete type

0
 
LVL 3

Author Comment

by:1ce
ID: 8124921
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv){
   int s, on;
   struct sockaddr dst;
   struct ifreq *ifr;
   char sendbuff[ 1024 ]="balbalbal";
   //char *sendaddr="10.10.255.255";
   /* Create And Set Up Socket */
   s = socket(AF_INET, SOCK_DGRAM, 0);
   on = 1;

   if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(int)) == -1) {
      printf("setsockopt SO_BROADCAST");
      return 0;
   }

   if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
      printf( "ioctl error" );
      return;
   }
   memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
   sendto(s, sendbuff, 1024, 0, (struct sockaddr *)&dst, sizeof (dst));

}

----------------------------------------------

It still giving the same error msg when compile. =(

>>if (ioctl(s,SIOCGIFBRDADDR, (char *) ifr) < 0){
socket_udp_bd.c:21: sizeof applied to an incomplete type

>>memcpy((char *) &dst, (char *)&ifr->ifr_broadaddr,sizeof(struct sockaddr_in));
socket_udp_bd.c:25: dereferencing pointer to incomplete type
socket_udp_bd.c:25: sizeof applied to an incomplete type

0
 
LVL 3

Author Comment

by:1ce
ID: 8125193
anyhow, i got it figured out, thanx
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month10 days, 1 hour left to enroll

762 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