Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-16-2007, 12:27 PM
|
#1
|
LQ Newbie
Registered: Mar 2007
Distribution: Debian
Posts: 4
Rep:
|
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?
|
|
|
03-28-2007, 04:56 AM
|
#2
|
Member
Registered: Jul 2003
Location: Lithuania
Posts: 45
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 08:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|