Jump to content
  • Advertisement
Sign in to follow this  

Sending out a broadcast packet in linux.

This topic is 4001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to send out a broadcast packet. I'm getting this error and i've been bangking my head on this for a while now. The following code below compiles but when I run it I get this error. sendto:: Invalid argument As you can see the dst contains the correct information but sendto function keeps giving me the Invalid argument. All the arguments are valid and correct it seems (ie, no null). Thanks for your help. Hopefully this makes sense.
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <string.h>
#include <stdio.h>

int main()
  int s, on = 1;
  struct sockaddr_in sin;
  struct sockaddr_in dst;
  struct ifconf ifc;
  struct ifreq *ifr;
  char buf[2048];
  char buf2[] = "HELLO MOTHER";
  int n;

  s = socket(AF_INET, SOCK_DGRAM, 0);
  if(s < 0)

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

  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(2190);

  if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)

  ifc.ifc_len = sizeof(buf);
  ifc.ifc_buf = buf;
  if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0)

  ifr = ifc.ifc_req;
  for (n=ifc.ifc_len/sizeof(struct ifreq); --n >= 0; ifr++)
    if(!strcmp(ifr->ifr_name, "eth0"))
      if(ioctl(s, SIOCGIFFLAGS, (char *)ifr) < 0)

  if (ifr->ifr_flags & IFF_BROADCAST)
    if(ioctl(s, SIOCGIFBRDADDR, (char *) ifr) < 0)
    memcpy((char *) &dst, (char *) &ifr->ifr_broadaddr, sizeof(ifr->ifr_broadaddr));
  } else
    printf("Not able to broadcast");

  printf("%s\n", inet_ntoa(dst.sin_addr));

  if(sendto(s, buf2, strlen(buf2), 0, (struct sockaddr *)&dst, sizeof(dst)) < 0)

  return 0;

Share this post

Link to post
Share on other sites
That's... interesting. Usually, you just set SO_BROADCAST, and send to the Mars address (ff.ff.ff.ff) which translates to the local subnet (because it's filtered at the gateway/router level). That's a lot more portable, and doesn't need OS-specific ioctls. It also doesn't have permission problems -- some OSes don't allow non-root to do ifconfig, or send raw messages, for example.

Share this post

Link to post
Share on other sites
Have u specified "dst"'s port?
In terms of UDP broadcast, u need specify a port number in order to send datagram.
And, UDP broadcast has a special broadcasting address: That define is INADDR_BROADCAST, u haven't refered in ur program.

Here is a simple UDP broadcast code as follows.

BOOL bBroadcast;
char* sMsg = "I love u";

s = socket(AF_INET, SOCK_DGRAM, 0);
bBroadcast = TRUE;
setsockopt(s, SOL_SOCKET, SO_BROADCAST, (char*)&bBroadcast, sizeof(BOOL));
dst.sin_family = AF_INET;
dst.sin_adddr.s_addr = inet_addr(INADDR_BROADCAST);
dst.sin_port = htons(8000);
sendto(s, sMsg, strlen(sMsg), 0, (SOCKADDR*)&dst, sizeof(dst));

In fact, I myself don't like Broadcast, if concurrently a lot of processes send broadcast message, the network immediately slows to a standstill and everyone gets frustrated.

Share this post

Link to post
Share on other sites
If I may ask, why are you sending broadcast packets? Most uses I can think of revolve around network discovery, and libavahi handles that exceptionally well.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!