title: Android UDP Multicast(Server) to C# WPF(Client) sometimes works only with registered multicast ip
body: <p>I am working on a simple app which sends camera byte stream to c# wpf app using UDP Multicast. I am able to achieve this using UDP Broadcasting.</p>
  
  <p>But what I want is to connect it using UDP multicast but its working for me only if I am using registered multicast IP's like "224.0.0.1".</p>
  
  <p>About my Android Code:
  I have written a Service for udp connection which I can call from any activity/fragment. This Service has functions for sending images and texts.</p>
  
  <p><strong>On Camera PreviewCallback</strong></p>
  
  <pre><code>private PreviewCallback previewCb = new PreviewCallback() {
          public void onPreviewFrame(byte[] frame, Camera c) {
              mService.initSocket();
              mService.sendImages(frame);
              c.addCallbackBuffer(frame);
          }
      };
  </code></pre>
  
  <p><strong>Camera Setup:</strong></p>
  
  <pre><code>Camera.Parameters p = camera_.getParameters();
          p.setPreviewSize(procSize_.width, procSize_.height);
          //p.setPreviewFormat(ImageFormat.NV21);
          p.setPreviewFpsRange(targetMaxFrameRate, targetMinFrameRate);
          camera_.setParameters(p);
  
          PixelFormat pixelFormat = new PixelFormat();
          PixelFormat.getPixelFormatInfo(ImageFormat.RGB_565, pixelFormat);
          int bufSize = procSize_.width * procSize_.height * pixelFormat.bitsPerPixel / 8;
          byte[] buffer = null;
          for(int i = 0; i &lt; bufNumber; i++) {
              buffer = new byte[ bufSize ];
              camera_.addCallbackBuffer(buffer);
          }
          camera_.setPreviewCallbackWithBuffer(cb);
  </code></pre>
  
  <p><strong>Send Images function:</strong></p>
  
  <pre><code>    while(bRunning) {
        int sessionNumber = 0;
        byte[] buff = msg;
        int packets = (int) Math.ceil(buff.length / (float) DATAGRAM_MAX_SIZE);
        for (int i = 0; i &lt; packets; i++) {
      DatagramPacket packet = new DatagramPacket(data, data.length, localaddress, 11000);
  sessionNumber = sessionNumber &lt; MAX_SESSION_NUMBER ? ++sessionNumber : 0;
      if(sessionNumber == packets){
                      bRunning = false;
                   }
             }
      }      
  </code></pre>
  
  <p><strong>About my C# code:</strong>
  I have created a UDPMulticast listener:</p>
  
  <pre><code>IPAddress multicastIP= IPAddress.Parse("224.0.0.1");
  UdpClient udpClient = new UdpClient();
  IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, port);
  udpClient.MulticastLoopback = true;
  udpClient.Client.Bind(remoteEndPoint);
  udpClient.JoinMulticastGroup(multicastIP);
  byte[] finalBuffer = new byte[80000];
              while (true)
              {
                  byte[] buffer = new byte[1048];
                  buffer = mUDPClient.Receive(ref ipEndPoint);
                  if (buffer[5].Equals(0))
                  {
                      MemoryStream stream = new MemoryStream(finalBuffer);
                      imgPreview.Source = GetImage(stream);
                      finalBuffer = new byte[25000];
                      Array.Copy(buffer, finalBuffer, buffer[4]);
                  }
                  else
                  {
                      Array.Copy(buffer, finalBuffer, buffer[4]);
                  }
              }
  
  public BitmapImage GetImage(Stream iconStream)
          {
              var imgbrush = new BitmapImage();
              imgbrush.BeginInit();
              imgbrush.StreamSource = iconStream;
              imgbrush.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
              imgbrush.EndInit();
              var ib = new ImageBrush(imgbrush);
              return imgbrush;
          }
  </code></pre>
  
  <p>Even when I am connected I get an error at imgbrosh.EndIt() saying that the image format is not proper because of which even my first image doesn't get created.</p>
  
  <p>Any help is appreciated, thanks.</p>