Why UDP broadcast don't run?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Why UDP broadcast don't run?
# 1  
Old 12-10-2008
Question Why UDP broadcast don't run?

I use FreeBSD7.0, I want to use UDP broadcast,my code is following:
/*udpcli01.c*/
int
main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if (argc != 2)
err_quit("usage: udpcli <IPaddress>");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(13);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (SA *) &servaddr, sizeof(servaddr));
exit(0);
}

/*dg_cli.c*/
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0; /* null terminate */
fputs(recvline, stdout);
}
}

When I run above code,10.1.2.255 is broadcast address,and input a line,like following:
$./udpcli01 10.1.2.255
Hi

It should receive many machine response,but I don't receive any message!
If I ping 10.1.2.255,I can receive many machine response message,why my code can't receive message? How can I modify above code to receive message?

Thanks in advance!
# 2  
Old 12-10-2008
Stay away from directed broadcasts, they are dangerous. Use 255.255.255.255 instead.

Note originally 255.255.255.255 was intended to be used to broadcast to every node on the Internet. It was quickly found to be a bad idea and was changed to mean "limited broadcast" to be sent on the local link only. See RFC 1122.
# 3  
Old 12-14-2008
Hi, you need to set SO_BROADCAST option for the socket with 'setsockopt' function.
And you need suitable server to respond...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

Nc won't send udp broadcast!?

Greetings, I want to send broadcast udp from a script. This works but is not broadcast: echo -n "this is my message\r\n" | nc -u 192.168.0.12 5100 The broadcast version does not work: echo -n "this is my message\r\n" | nc -u 192.168.0.255 5100 Suggestions on the right way to do this... (2 Replies)
Discussion started by: anotherstevest
2 Replies

3. Shell Programming and Scripting

Background processes in a pipeline don't run asynchronously?

I'm wondering about what causes this behavior. Mainly, given the first case below, why the second changes the behavior of ''&'' to execute sequentially merely because the command group containing calls to f() is part of a pipeline. The 4th example bizarrely once again works as the first, even with... (2 Replies)
Discussion started by: ormaaj
2 Replies

4. Programming

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (6 Replies)
Discussion started by: moti12
6 Replies

5. IP Networking

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (0 Replies)
Discussion started by: moti12
0 Replies

6. Programming

How to broadcast a message ?

My problem definition is ,I have to send a message from one node in a network and it has to be broadcasted to all other nodes in the network.The program what I have given below will be running in all the nodes in the network.The same program should be capable of sending(broadcasting) and receiving.... (0 Replies)
Discussion started by: vigneshinbox
0 Replies

7. Shell Programming and Scripting

Run expect script in systems that don't support it out of box

Noob question .. My Java based application needs to change some user passwords based on some user actions. Since this application can run on Redhat AS2.1 / AS4.0 / Solaris 9 etc, the most safe and portable solution that I could think of was: Use expect. Now, expect is not available on all... (1 Reply)
Discussion started by: namityadav
1 Replies

8. UNIX Desktop Questions & Answers

broadcast

Can anybody hellp me troubleshoot this error. Using HP-Uxix B.10.X " *****ARPA/9000 NETWORKING***** Mon Jun 30 SAT 2003 20:18:12.xxxxxxx DISASTER Subsys: NS_LS_PROBE LOC:01001 Network NS_LS_PROBE DISASTER 1001, pid arp: ether address is broadcast for IP address zzzzzzz! " where... (1 Reply)
Discussion started by: SEB
1 Replies
Login or Register to Ask a Question