Andrei
Andrei

Reputation: 115

Xamarin Forms Artnet and UDP transmission

I'm creating a cross platform application that uses Artnet and UDP protocol to communicate with a device on the network. I know Artnet is also UDP.


Where it works:

  1. Windows OS:
  1. Android OS:
  1. iOS:

Don't understand why there's 0 communication when there's a router involved on Android and iOS. I tried all the suggested codes I could find online and ticked all the capabilities that I could find for Android and iOS. Wireshark shows there is transmission going on, but my App doesn't capture the packets.

Snipets:

var artnet = new ArtNetSocket(); // using System.Net.Sockets;
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
artnet.Bind(new IPEndPoint(LocalIP, 6454));
artnet.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

EndPoint localPort = new IPEndPoint(IPAddress.Any, Port);
ArtNetData data= new ArtNetData();
artnet.BeginReceiveFrom(recieveState.buffer, 0, recieveState.bufferSize, SocketFlags.None, ref localPort, new AsyncCallback(WhenRecieved), data);


private void WhenRecieved(IAsyncResult state)
{
            //1.Do something when received
            //2.Begin receive again

}

How I look for IPs:

            NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface Interface in Interfaces)
            {
                if (Interface.NetworkInterfaceType != NetworkInterfaceType.Loopback )
                {

                    UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
                    foreach (var info in UnicastIPInfoCol)
                    {
                        if (info.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IPsets.Add(new IPCouples(info.Address, info.IPv4Mask));
                            
                        }
                    }
                }
            }

It's so weird it's probably something simple...

Upvotes: 0

Views: 237

Answers (1)

Andrei
Andrei

Reputation: 115

Ok, because I was creating sockets for each IP group, packets weren't captured on public networks, unless the APP was on a Microsoft OS device. If I create a UDPClient and bind it to a port, instead of the localIP address, it will capture everything coming on that port, regardless of the sender's IP Address. If you check the IP address of the socket, it will be 0.0.0.0:PortNumber.

    public class Something: UdpClient
    {
       public int LocalPort;
       public Something(int LocalPort) : base(LocalPort)
        {

            EnableBroadcast = true;
            this.LocalPort = LocalPort;
            StartListening();
        }

        private void StartListening()
        {
            //IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
            
            try
            {

                StateObject state = new StateObject();
                state.workSocket = Client;
                BeginReceive(received, state);
                
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
        }

        private void received(IAsyncResult ar)
        {
            
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            StateObject state = (StateObject)ar.AsyncState;

            var message = EndReceive(ar, ref remoteEndPoint);

            if (message.Length > 0)
            { 
                var messageString = (Encoding.UTF8.GetString(message));
                    
                //Do Something
            }
            StartListening();



        }

        public SendToAll(string message)
        {
         var callMessage = Encoding.ASCII.GetBytes(message);
                        UnicastIPAddressInformationCollection UnicastIPInfoCol = Interface.GetIPProperties().UnicastAddresses;
                        foreach (UnicastIPAddressInformation UnicatIPInfo in UnicastIPInfoCol)
                        {
                            if (UnicatIPInfo.Address.AddressFamily == AddressFamily.InterNetwork)
                            {

                                EnableBroadcast = true;
                                Send(callMessage, callMessage.Length, new IPEndPoint(GetBroadcast(UnicatIPInfo.Address,UnicatIPInfo.IPv4Mask) , LocalPort));

                            }
                        }
      }

      private IPAddress GetBroadcast(IPAddress someAddress, IPAddress someMask)
      {                

           //Do something to get broadscat
           return Broadcast;
      }


}
    public class StateObject
    { 
        public Socket workSocket = null;
        public const int BufferSize = 256;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();
    }

It works on all platform. Carefull which port you use.

Upvotes: 0

Related Questions