LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2007, 12:27 PM   #1
inakizi
LQ Newbie
 
Registered: Mar 2007
Distribution: Debian
Posts: 4

Rep: Reputation: 0
Question Problem with Broadcast UDP in two ethernet machine


I want to receive a broadcast UDP message in my linux machine. I'm using Debian 2.4.27-3-386 Kernel. Is a very simple C program (I'll make it short here):

int sockfd;
struct sockaddr_in my_addr; // my address struct sockaddr_in their_addr; // connector’s socklen_t addr_len;
int numbytes;
char buf[MAXBUFLEN];


if ((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // Listen trough every interface
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct


// BIND to an specific address:
if (bind(sockfd, (struct sockaddr *)&my_addr,
sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
addr_len = sizeof(struct sockaddr);

while(1) {

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

If I have only one ethernet port (let's say I do ifdown eth1). This code works perfect when I send a broadcast (255.255.255.255) message from another machine. If now I do ifdown eth0 ifup eth1, it also works fine. The problem is that with both interfaces UP the program only receives broadcast that came from the latest interface that was up. I want to be able to get broadcast messages from both interfaces. Has anyone know how to do this?
 
Old 03-28-2007, 04:56 AM   #2
G-Fox
Member
 
Registered: Jul 2003
Location: Lithuania
Posts: 45

Rep: Reputation: 15
Hi, I write program with perl and my linux box have two NIC's. I try get broadcast packets on both NIC's and get only then configure each NIC to senders subnet.

server code:
#!/usr/bin/perl
use strict;
use Socket;

my $proto = getprotobyname ('udp');
my $l_host = inet_aton ('0.0.0.0');
my $l_port = '60000';
my $l_addr = sockaddr_in ($l_port, $l_host);
socket (SOCKET, PF_INET, SOCK_DGRAM, $proto);
bind (SOCKET, $l_addr);
my $data;

while ( 1 ) {
my $from_who = recv (SOCKET, $data, 1500, 0);
if ($from_who) {
my ($the_port, $the_ip) = sockaddr_in( $from_who );
print 'Received from ' . inet_ntoa( $the_ip ) . ": " . length($data) . " Bytes\n\n";
}
else {
warn "Problem with recv: $!\n";
}
}
close(SOCKET);

-----------------------
client code:

#! /usr/bin/perl -w
use strict;
use Socket;

my $s_port = '60001';
my $r_port = '60000';
my $l_host = inet_aton('192.168.0.2');
my $proto = getprotobyname( 'udp' );
my $r_host = inet_aton('255.255.255.255');
my $destination = sockaddr_in( $r_port, $r_host );
socket( UDP_SOCK, PF_INET, SOCK_DGRAM, $proto );
setsockopt ( UDP_SOCK, SOL_SOCKET, SO_BROADCAST, 1);
bind(UDP_SOCK, sockaddr_in($s_port, $l_host));
my $data = shift || "test";
send( UDP_SOCK, $data, 0, $destination );
close UDP_SOCK;

------------------------

I hope this will help you
 
  


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
UDP broadcast receiver estratos Programming 1 03-14-2007 08:58 AM
broadcast (udp) socket - multi processes emarri Programming 1 09-25-2006 07:06 AM
udp broadcast from one subnet to another thru a linux router siva_bhavani Linux - Networking 1 12-30-2004 11:20 AM
how to broadcast UDP datagrams sokar Programming 2 02-14-2004 06:37 PM
Strange Ethernet Problem - New machine GAVollink Linux - Networking 2 04-11-2002 10:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 08:01 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
Open Source Consulting | Domain Registration