LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



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

Rep: Reputation: 15
UDP broadcast receiver


Hi again,

I write this post after getting some answers here:
http://www.linuxquestions.org/questi...d.php?t=535017
http://www.linuxquestions.org/questi...d.php?t=535308

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: http://beej.us/guide/bgnet/examples/listener.c
to listen for udp broadcasts on a given port. Here you have the code (I've changed the port number):

Code:
#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) {
		perror("socket");
		exit(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) {
		perror("bind");
		exit(1);
	}

	addr_len = sizeof(struct sockaddr);
	if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
		(struct sockaddr *)&their_addr, &addr_len)) == -1) {
		perror("recvfrom");
		exit(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);

	close(sockfd);

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

PC network data:
IP address = 192.168.1.17
Gateway = 192.168.1.1
Netmask = 255.255.255.0
Broadcast = 192.168.1.255

The problem arrives when any of the network applications broadcasts UDP packets to a different address (Ex: 192.255.255.255). 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?

Thanks,

Daniel.
 
Old 03-14-2007, 08:58 AM   #2
debulu
Member
 
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 192.168.1.255 will broadcast message to all those in the subnet 192.168.1.0
listening on that port.

Hope helps
 
  


Reply


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 07:06 AM
UDP: Short Packets: and UDP bad checksum: entries in dmesg minutes2memories Linux - Networking 2 02-26-2006 08:28 PM
udp broadcast from one subnet to another thru a linux router siva_bhavani Linux - Networking 1 12-30-2004 11:20 AM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 06:43 AM
how to broadcast UDP datagrams sokar Programming 2 02-14-2004 06:37 PM


All times are GMT -5. The time now is 09:47 PM.

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