UDP Broadcast fails
UDP Broadcast fails
- Subject: UDP Broadcast fails
- From: Kai Fehrs <email@hidden>
- Date: Thu, 10 Sep 2009 14:16:36 +0200
Hello,
I have some trouble sending UDP messages to the broadcast address
INADDR_BROADCAST (255.255.255.255). My program runs without an error. But
there is no packet sent to the network. Wireshark does not display anything
and the "Sent Packets" counter remains the same.
What's goind wrong?
Thank you in advance.
/* udpserver.c */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int sock;
struct sockaddr_in server_addr;
int port = 12345;
int ret;
int opt;
char buf[20] = "Hallo Welt!";
struct sockaddr_in addr;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr("192.168.10.75");
bzero(&(server_addr.sin_zero),8);
/* enable broadcast messages */
opt = 1;
ret = setsockopt(sock, SOL_SOCKET, SO_BROADCAST,&opt, sizeof(opt));
if(ret == -1)
{
perror("enable broadcast");
exit(-1);
}
/* enable local address reuse */
opt = 1;
ret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt,sizeof(opt));
if(ret == -1)
{
perror("enable reuse");
exit(-1);
}
/* set packets' time to live */
opt = 1;
ret = setsockopt(sock, IPPROTO_IP, IP_TTL, &opt, sizeof(opt));
if(ret == -1)
{
perror("set IP TTL");
exit(-1);
}
/* bind socket */
ret = bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if (ret == -1)
{
perror("bind");
exit(-1);
}
while(1) {
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_BROADCAST;
addr.sin_port = htons(38202);
ret = sendto(sock,
buf,
strlen(buf),
0, (struct sockaddr*)&addr,
sizeof (addr));
if( ret == -1)
{
perror("sendto");
exit(-1);
}
else
{
printf("%d bytes sent\n", ret);
sleep(1);
}
}
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden