title: Receiving broadcast packet addressed to 255.255.255.255 in C++
body: <p>I have a device that is discovered by sending a broadcast packet to 255.255.255.255 on port 4930 and the device responds by sending a packet back to 255.255.255.255 on port 4930.</p>
  
  <p>I have a snippet of C++ code which can send a packet to 255.255.255.255 on port 4930 (both source and destination port), but it can't receive a packet back from the broadcast address 255.255.255.255.</p>
  
  <p>I can see the device is working fine, wireshark can see the packets coming back and forth and the propriety software supplied with the device can discover the device just fine, the problem is with the C++ program so please keep on topic with your responses.</p>
  
  <p>Now, as I have said I can send a packet just find, but firstly I can't bind to the IP address 255.255.255.255 to receive the packets. I can change the multicast address to 239.255.255.250 and the socket will bind but I need the address 255.255.255.255.</p>
  
  <p>My snippet of code is below, I am using VC++2010</p>
  
  <pre><code>bool CPTUProgramDlg::FindPTU(u_short port, const char * Destaddress){
      {
          //Data to send
          char packet_data[10] = {0x44,0x43,0x55,0x44,0x5f,0x50,0x49,0x4e,0x47,0x00};
          int packet_size=10;
  
          SOCKET sock;
          struct sockaddr_in addr;
  
          sock = socket(AF_INET, SOCK_DGRAM, 0);
  
          // set SO_BROADCAST on a socket to true (1): (so we can transmit to 255 addr)
          //In order to use broadcast the options of socket must change
          char broadcastON = 1;
          setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &amp;broadcastON, sizeof broadcastON);
  
          if (sock &lt; 0)
              return false;
  
          addr.sin_family = AF_INET;
          addr.sin_port = htons(port);
          addr.sin_addr.s_addr = inet_addr(Destaddress); // Specify dest IP
  
          sendto(sock, packet_data, packet_size, 0, (struct sockaddr*)&amp;addr, sizeof(addr));
  
          if (bind(sock,(struct sockaddr*) &amp;addr,sizeof(addr)) != -1){
              char Buff[512];
              recv(sock,Buff,512,0);
          }
  
          closesocket(sock);
      }
  
      return 1;
  }
  </code></pre>
  
  <p>Wireshark screenshot to prove packets are being send:</p>
  
  <p><a href="https://i.stack.imgur.com/rYWqL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rYWqL.jpg" alt="enter image description here"></a></p>