12-10-2008
29,
0
Join Date: Jan 2002
Last Activity: 22 January 2011, 4:41 AM EST
Posts: 29
Thanks Given: 0
Thanked 0 Times in 0 Posts
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!