Solved

UDP Broadcast Message in C using Linux

Posted on 2004-09-09
7
1,846 Views
Last Modified: 2009-12-11
Hello
Can somebody give me a simple example on sending and receving a UDP broadcast message?
I am using C and Linux.
0
Comment
Question by:sctccomm
[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
  • 3
  • 2
7 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12024136
Its nearly the same as sending normal UDP messages. .. you need to use setsockopt for setting the socket to broadcast

SO_BROADCAST
Set or get the broadcast flag. When enabled, datagram sockets receive packets sent to a broadcast address and they are allowed to send packets to a broadcast address. This option has no effect on stream-oriented sockets.

man 2 setsockopt
man 7 socket

here is some theory on it
http://elqui.dcsc.utfsm.cl/util/redes/www.cisco.com/univercd/cc/td/doc/cisintwk/ics/cs006.htm

Source code for an irdp daemon is available by anonymous FTP at ftp.cisco.com.
0
 

Author Comment

by:sctccomm
ID: 12029178
This is my code.  I use Ethernel to monitor the packet and I got a "Malformed Packet WCCP" on the packet I send

#include <stdio.h>            /* Standard input/output definitions */
#include <stdlib.h>
#include <string.h>            /* String function definitions */
#include <errno.h>
#include <unistd.h>            /* UNIX standard function definitions */
#include <fcntl.h>            /* File control definitions */

#include <signal.h>
#include <sys/time.h>
#include <sys/msg.h>      /* Message Queue */

#include <sys/types.h>
#include <sys/socket.h>      /* Ethernet socket */
#include <netinet/in.h>      
#include <arpa/inet.h>

#define LOCAL_PORT      3820
#define REMOTE_PORT      3820

int remote_sock_fd;                  //Ethernet port file descriptor
struct sockaddr_in remote_addr;      
int remote_addr_len, rx_addr_len;

unsigned int number=0;

void send_data(int signal)
{
      char tx_buf[128];
      int send_size;

      printf("Send Number:%d\n",number);        
      tx_buf[0] = number++;
      
      send_size = sendto(remote_sock_fd, tx_buf, 1, 0, (struct sockaddr *)&remote_addr, remote_addr_len);
      printf("Send Size:%d\n",send_size);
}


int main(int argc, char *argv[])
{
      const int on = 1;
      struct itimerval itval;

      char rx_buf[128];
      int rx_size;
      
      bzero(&remote_addr, sizeof(remote_addr));
      remote_addr.sin_family = AF_INET;                 // host byte order
      remote_addr.sin_port = htons(REMOTE_PORT);       // short, network byte order
      remote_addr.sin_addr.s_addr = inet_addr("192.168.254.255");
      remote_addr_len = sizeof(remote_addr);

      remote_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
      if (remote_sock_fd == -1)
      {
          perror("Socket Error");
          exit(1);
        }
      
      setsockopt(remote_sock_fd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
      
      signal(SIGALRM, send_data);

      itval.it_interval.tv_sec = 2;
      itval.it_interval.tv_usec = 000000;

      itval.it_value.tv_sec = 2;
      itval.it_value.tv_usec = 000000;
      
      setitimer(ITIMER_REAL,&itval,0);
      
      while(1)
      {
            pause();
/*            rx_size = recvfrom(remote_sock_fd, rx_buf, 1, 0,  (struct sockaddr *)&remote_addr, &rx_addr_len);
            if(rx_size > 0)
            {
                  printf("Receive Number:%d\n",rx_buf[0]);
            }
*/
      }
      
      exit(EXIT_SUCCESS);
}
0
 

Author Comment

by:sctccomm
ID: 12029188
I mean I use Ethereal to monitor the packets
0
Industry Leaders: 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!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12051557
Hi sctccomm,

Were you able to solve the problem? If yes, then pls post the solution. If no and you are still looking for a solution, I would suggest leaving it open for a while.

GhostMod,

In case of no response, I am ok with delete/Refund

sunnycoder
0
 

Author Comment

by:sctccomm
ID: 12055763
I found that I need to "bind" the socket and after that, everything runs fine.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12083904
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

Industry Leaders: 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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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