Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-08-2007, 03:36 AM   #1
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
UDP broadcast receiver

Hi again,

I write this post after getting some answers here:

I've decided to open a new thread once I know a little more about the problem - I'm now sure that it's a programming issue so there is no reason to continue the discussion in the "Networking" board.

I'm running this simple application:
to listen for udp broadcasts on a given port. Here you have the code (I've changed the port number):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MYPORT 3865	// the port users will be connecting to
#define MAXBUFLEN 512

int main(void)
	int sockfd;
	struct sockaddr_in my_addr;	// my address information
	struct sockaddr_in their_addr; // connector's address information
	socklen_t addr_len;
	int numbytes;
	char buf[MAXBUFLEN];

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {

	my_addr.sin_family = AF_INET;		 // host byte order
	my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
	my_addr.sin_port = htons(MYPORT);	 // short, network byte order
	memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

	if (bind(sockfd, (struct sockaddr *)&my_addr,
		sizeof(struct sockaddr)) == -1) {

	addr_len = sizeof(struct sockaddr);
	if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
		(struct sockaddr *)&their_addr, &addr_len)) == -1) {

	printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
	printf("packet is %d bytes long\n",numbytes);
	buf[numbytes] = '\0';
	printf("packet contains \"%s\"\n",buf);


	return 0;
The application correctly receives UDP packets broadcasted to the (Ubuntu) machine's broadcast address and also to

PC network data:
IP address =
Gateway =
Netmask =
Broadcast =

The problem arrives when any of the network applications broadcasts UDP packets to a different address (Ex: Then, the UDP receiver (above code) drops those packets. I know that this has nothing to do with the kernel as Ethereal can see the dropped packets on the same machine.

Is there anything that I could add into the code in order to accept packets sent to any broadcast address within my LAN?


Old 03-14-2007, 07:58 AM   #2
Registered: May 2006
Location: India
Distribution: Redhat
Posts: 49

Rep: Reputation: 15
You obviously can do broadcast to a subnet in the LAN.

The broadcast address must be the <NetWork><.255.255>

e,g will broadcast message to all those in the subnet
listening on that port.

Hope helps


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
broadcast (udp) socket - multi processes emarri Programming 1 09-25-2006 06:06 AM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 07:28 PM
udp broadcast from one subnet to another thru a linux router siva_bhavani Linux - Networking 1 12-30-2004 10:20 AM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM
how to broadcast UDP datagrams sokar Programming 2 02-14-2004 05:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration