title: Multicasting C++ multiple computers on the same LAN
body: <p>i am working on an assignment for college, i was asked to implement a chat for the LAN of my college.
  I am in the process of working with multicast for sending a same message to all registered users on the group. My sender is developed on C++ and the receivers on java. When testing on the same computer the code that i am attaching works fine, sender sends, and receiver receives, but when running client on another computer it does not receive the messages sent.</p>
  
  <p>Server:</p>
  
  <pre><code>int main(){
  
  /** MC socket **/
  
  struct sockaddr_in groupSock;
  groupSock.sin_family = AF_INET;
  groupSock.sin_addr.s_addr = inet_addr("225.5.4.30");
  groupSock.sin_port = htons(54321);
      bzero(&amp;(groupSock.sin_zero),8);
  
  int mcsock;
  if ((mcsock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
          perror("Socket MC");
          exit(1);
      }
  
  int nroM = 0;
  while(1)
  {
      fflush(stdout);
  
      stringstream resp; 
      resp &lt;&lt; "Mensaje multicast: " &lt;&lt; nroM &lt;&lt; "\n";
      cout &lt;&lt; resp.str();
      /* Send a message to the multicast group specified by the*/
      /* groupSock sockaddr structure. */
      /*int datalen = 1024;*/
      if(sendto(mcsock, resp.str().c_str(), strlen(resp.str().c_str()), 0, (struct sockaddr*)&amp;groupSock, sizeof(groupSock)) &lt; 0)
      perror("Sending datagram message error");
      nroM++;
      sleep(2);
  }
  
  close(mcsock);
      return 0;
  
  } 
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>class UDPCliente {
  public static void main(String args[]) throws Exception{
  
  InetAddress address = InetAddress.getByName("225.5.4.30");
  // Create a buffer of bytes, which will be used to store
  // the incoming bytes containing the information from the server.
  // Since the message is small here, 256 bytes should be enough.
  byte[] buf = new byte[256];
  // Create a new Multicast socket (that will allow other sockets/programs
  // to join it as well.
  try (MulticastSocket clientSocket = new MulticastSocket(54321)){
      //Joint the Multicast group.
      clientSocket.joinGroup(address);
      while (true) {
      // Receive the information and print it.
      DatagramPacket msgPacket = new DatagramPacket(buf, buf.length);
      clientSocket.receive(msgPacket);
      String msg = new String(buf, 0, buf.length);
      System.out.print(msg);
      }
  } catch (IOException ex) {
      ex.printStackTrace();
  }
  }
  </code></pre>
  
  <p>Just for extra information, this code with the appropiate imports and includes compile and run.</p>
  
  <p>Thank you!!</p>