UDP sockets in ad hoc network (Ubuntu 9.10)

Posted by Ekhiotz on Stack Overflow See other posts from Stack Overflow or by Ekhiotz
Published on 2010-06-01T13:21:49Z Indexed on 2010/06/01 14:53 UTC
Read the original article Hit count: 272

Filed under:
|
|
|
|

Hi!

I am using BSD sockets in Ubuntu 9.10 to send UDP packets in broadcast with the following code:

        sock_fd = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
        //sock_fd=socket(AF_INET,SOCK_DGRAM,0);
        receiver_addr.sin_family = PF_INET;
        //does not send with broadcast in ad hoc
        receiver_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
        inet_aton("169.254.255.255",&receiver_addr.sin_addr);
        receiver_addr.sin_port = htons(port);
        int broadcast = 1;
           // this call is what allows broadcast packets to be sent:
           if (setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &broadcast,
               sizeof broadcast) == -1) {
               perror("setsockopt (SO_BROADCAST)");
               exit(1);
           }
        ret=sendto(sock_fd, packet, size, 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));

Note that is not all the code, it is only to have an idea.

The program sends all the data with INADDR_BROADCAST if I am connected to an infrastructure wireless network. However, if my laptop is connected to an ad-hoc network, it is able to receive all the data, but not to send it. I have solved the problem using the 169.254.255.255 broadcast address, but I would like to know what is going on.

Thank you in advance!

© Stack Overflow or respective owner

Related posts about c

    Related posts about linux