UDP sockets on an ad hoc network (Ubuntu 9.10)

I use BSD sockets in Ubuntu 9.10 to send UDP packets in broadcast mode 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 this is not all code; it should only have an idea.

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

Thank you in advance!

+3
source share
1 answer

Do you have multiple adapters (i.e. wired Ethernet, WLAN)?

(INADDR_BROADCAST) IP- . . IP- , . , . , UDP . ( , DHCP.)

UDP , . , 169.254.255.255 AUTOIP.

(SOL_SOCKET, SO_BROADCAST) , .

+2

Source: https://habr.com/ru/post/1747983/


All Articles