Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

UDP Broadcast Message in C using Linux

Posted on 2004-09-09
7
Medium Priority
?
1,870 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
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

Stressed Out?

Watch some penguins on the livecam!

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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

671 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