?
Solved

sendto() broadcast datagram packet

Posted on 2003-03-10
5
Medium Priority
?
1,778 Views
Last Modified: 2008-03-10
I am trying to broadcast a simple datagram packet over the network using the sendto() function on a linux box.

        int sckt;
     struct sockaddr_in dest_addr;
     sckt=socket(AF_INET,SOCK_DGRAM,0);
     dest_addr.sin_family=AF_INET;
     dest_addr.sin_port=htons(5051);
     inet_aton("192.168.1.255",&dest_addr.sin_addr.s_addr);
     memset (&(dest_addr.sin_zero),'\0',8);
        if (sendto (sckt,"test",4,0,&dest_addr,sizeof(struct sockaddr_in))==-1)
          perror ("sendto");


perror returns Permission Denied

The above code works if I send it to just one address, but not with a broadcast address.

How can I do a broadcast

Thanks
jw
0
Comment
Question by:wilch101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
jcaldwel earned 150 total points
ID: 8105464
Try the following:

setsockopt(sck, SOL_SOCKET, SO_BROADCAST, 1)
0
 
LVL 1

Author Comment

by:wilch101
ID: 8105516
setsockopt(sck, SOL_SOCKET, SO_BROADCAST, 1)

perror on this function returns Bad Address

0
 
LVL 1

Expert Comment

by:jcaldwel
ID: 8105566
Sorry, I believe you have to bind the socket for this to work:

 if( bind (sck, (void *)&dest_addr, sizeof(dest_addr)) == -1)
    perror( "bind" );

Then do the setsockopt line.

0
 
LVL 1

Author Comment

by:wilch101
ID: 8105625
Sorry, but that didn't help.  

Still get the same error Bad Address.
0
 
LVL 1

Author Comment

by:wilch101
ID: 8105699
Actually it works by setting an int variable to 1 and passing its pointer in the function instead of 1.

Thanks
jw
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question