Deltarion
Deltarion

Reputation: 21

UWP App stops receiving UDP Multicast messages

I am currently running into a very strange problem. I am developping an application for Microsoft HoloLens with Unity 2017.4.2f2. The problem is that my code works well, but the HoloLens just won't receive any more UDP messages from my python server after few runs of my app.

Here is the code for the DatagramSocket connection :

private HostName inetAddr;
private DatagramSocket sock;
private int inetPort;
private const int BUFFER_SIZE = 1024;
private byte[] buffer;

private async Task ConnectSocket()
{
    sock = new DatagramSocket();
    sock.MessageReceived += Socket_MessageReceived;
    sock.Control.MulticastOnly = true;
    sock.Control.InboundBufferSizeInBytes = BUFFER_SIZE;
    await sock.BindServiceNameAsync(inetPort.ToString());
    sock.JoinMulticastGroup(inetAddr);
    // Send empty message to initialize communication
    await SendWithExistingSocket("");
}

private async Task SendWithExistingSocket(string text)
{
    if (sock != null)
    {
        Stream stream = (await sock.GetOutputStreamAsync(inetAddr, inetPort.ToString())).AsStreamForWrite();
        using (var writer = new StreamWriter(stream))
        {
            writer.WriteLine(text);
            await writer.FlushAsync();
        }
    }
}

private async void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
    using (Stream streamIn = args.GetDataStream().AsStreamForRead(BUFFER_SIZE))
    {
        await streamIn.ReadAsync(buffer, 0, BUFFER_SIZE);
    }
    IPAddress address = IPAddress.Parse(inetAddr.RawName);
    IPEndPoint from = new IPEndPoint(address, int.Parse(sender.Information.LocalPort));
    //Custom function for dealing with packet
    HandlePacket(buffer, from);
}

As I said, this problem is very strange because this code actually works, until HoloLens just stop receiving UDP Datagrams (function Socket_MessageReceived not called). In my example inetAddr = 224.3.0.5 and inetPort = 7667

What is important to add, is that my app restarts receiving UDP messages after a reboot of the HoloLens from the device portal.

I don't have this problem when I'm deploying my UWP app on my local machine. Only when I'm deploying my app to the HoloLens. I also have an implementation of this communication with UdpClient class (for testing in the editor), which works very well and doesn't behave like this.

What I already tried :

Any ideas why ? Thank You

Upvotes: 2

Views: 613

Answers (1)

Adan Pacheco
Adan Pacheco

Reputation: 1

Don't use Using with your writer and receiver because that will kill the communication with your listeners. I had that problem, and it was driving me crazy until I do that. Now it works like a charm.

I receive the packages like this:

bool refresh = false;
bool sound = false;
string response;
uint stringLength = args.GetDataReader().UnconsumedBufferLength;
response = args.GetDataReader().ReadString(stringLength);

and write the packages like this:

IOutputStream outputStream;
HostName remoteHostname = new HostName("224.3.0.5");

outputStream = await socketinstance.GetOutputStreamAsync(remoteHostname, ClientPortNumber);

DataWriter writer = new DataWriter(outputStream);
writer.WriteString(request);
await writer.StoreAsync();

Upvotes: 0

Related Questions