Beispiel #1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);

            try
            {
                InetAddress    multicastAddress = InetAddress.GetByName("192.168.2.229");
                DatagramSocket socket           = new DatagramSocket(5004);
                byte[]         buff             = new byte[18];

                DatagramPacket packet = new DatagramPacket(buff, buff.Length);
                socket.ConnectAsync(multicastAddress, 5008);
                socket.ReceiveAsync(packet);
            }
            catch (Exception e)
            {
            }
        }
Beispiel #2
0
        public async Task <UdpMessage> ReceiveAsync()
        {
            //TODO: move socket to constructor?
            try
            {
                socket.SoTimeout = 500;

                byte[] recvBuf = new byte[packageSize];

                using (DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.Length))
                {
                    await socket.ReceiveAsync(packet);

                    var message = Encoding.ASCII.GetString(packet.GetData());

                    return(new UdpMessage(packet.Address.ToString(), message));
                }
            }
            catch (Java.Net.SocketTimeoutException ex)
            {
                return(default);