Go Premium for a chance to win a PS4. Enter to Win

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

UDP Broadcast Message in C using Linux

Hello
Can somebody give me a simple example on sending and receving a UDP broadcast message?
I am using C and Linux.
0
sctccomm
Asked:
sctccomm
  • 3
  • 2
1 Solution
 
sunnycoderCommented:
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
 
sctccommAuthor Commented:
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
 
sctccommAuthor Commented:
I mean I use Ethereal to monitor the packets
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
sunnycoderCommented:
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
 
sctccommAuthor Commented:
I found that I need to "bind" the socket and after that, everything runs fine.
0
 
ee_ai_constructCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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