Question

First off, I am new to Android and dot42. I have built a UDP Listener class, which is working fine in a Windows application. I am using the System.Net.Sockets.UdpClient class.

Now, I have tried to use my UDP Listener class in a dot42 project, but I get the error message

Type System.Net.Sockets.UdpClient not found.

I guess, this class is not available within dot42?

Is there a way I can use the same code below (or only a few modifications) for Android apps?

using System;
using System.Net;
using System.Net.Sockets;

namespace FSInterface
{
    public class UDPReceiver
    {
        private UdpClient _udpRx;
        private IPEndPoint ep;
        private int _port;

        public delegate void BytesReceive(byte[] buffer);
        public event BytesReceive OnBytesReceive;

        public UDPReceiver(int port)
        {
            _port = port;
            _udpRx = new UdpClient();
            _udpRx.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            _udpRx.ExclusiveAddressUse = false;
            ep = new IPEndPoint(IPAddress.Any, _port);
            _udpRx.Client.Bind(ep);
            _udpRx.BeginReceive(new AsyncCallback(ReceiveCallback), null);
        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            _udpRx.BeginReceive(new AsyncCallback(ReceiveCallback), null);

            byte[] buffer = _udpRx.EndReceive(ar, ref ep);

            if (OnBytesReceive != null)
            {
                OnBytesReceive(buffer);
            }
        }
    }
}
Was it helpful?

Solution

Dot42 is very promissing project, what is still missing is more complete set of BCL.

In this case you should use java classes. You can find many examples of client UDP code in java. If you want to share code between regular .net and dot42, you have to use interface to udp methods with separate implementations for regular .NET and Dot42.

OTHER TIPS

A little update. We released the .NET implementation under Apache License 2.0. You may now add missing types yourself. https://github.com/dot42/api.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top