Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

UDP messages through simulator

$
0
0

I'm trying to have a Xamarin.iOS application send some random UDP data to a server, which should reply echoing that data and registering the incoming address for later use. To do this, I periodically query a STUN service to get my address and port, then send it to the server with a webapi call and have it reply, in turn, with the UDP port I should be sending the random data to.
The problem is that my code works fine on a Windows Forms, but when I use that exact same code on Xamarin.iOS it just won't. The server does listen on the port it sent to the client, but no data is ever received.

I should mention that I'm using an iPad Pro (12.9-inch) (2nd generation) iOS 11.2 simulator, accessed on a Mac Mini via VS Enterprise. Here is my client-side code:

// Get endpoint via STUN
using (System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
    socket.Bind(new IPEndPoint(IPAddress.Any, 0));

    STUN_Result res = STUN_Client.Query("stun-eu.3cx.com", 3478, socket);
    if (res.NetType != STUN_NetType.UdpBlocked)
        EndPoint = res.PublicEndPoint;
}

// Get the port to use (if not already set, otherwise the current port will be returned by the server)
WebCall<int> portCall = new WebCall<int>("SignatureWebApi/Pair", new { Code = code, EndPoint = EndPoint.ToString() });
Port = portCall.Invoke();

// Send random data
IPEndPoint destination = new IPEndPoint(IPAddress.Parse(ADDRESS), Port);
Client = new UdpClient(EndPoint.Port);
int sz = Random.Next(1, 16);
byte[] randomData = new byte[sz];
for (int i = 0; i < sz; i++) randomData[i] = Convert.ToByte(Random.Next(byte.MinValue, byte.MaxValue));

Client.BeginSend(randomData, sz, destination, r =>
{
    Client.BeginReceive(rc =>
    {
        IPEndPoint source = new IPEndPoint(IPAddress.Any, 0);
        byte[] check = Client.EndReceive(rc, ref source);
        if (!Enumerable.SequenceEqual(randomData, check))
            throw new Exception();
        Client.Close();
    }, null);
}, null);

WebCall is a class wrapping a RestSharp call, and it has worked seamlessly for the past 3 months. Also, like I said, this code works just fine in a Windows Forms application. The only thing that might be at fault is ADDRESS (hard-coded to be 127.0.0.1), which is the same address used by WebCall (and therefore RestSharp) to contact the server via WebApi; although the server is not running on the Mac (thus its address should not be 127.0.0.1), it is perfectly reachable via this address from WebCall. Also, I tried having the server communicate its own full endpoint (obtained, once more, via STUN), but it hasn't worked either.
Finally, the Mac firewall is completely turned off, and the Windows firewall of the PC I'm running the server in is set to allow incoming and outgoing connections through all ports in the range I'm using (20000 through 30000).
What is the problem here? What should I do to have the server receive the client's UDP packets?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>