3

I'm trying to pass messages with NetMQ in C# UWP to python.

The python acts as Subscriber, and the C# as Publisher.

When I use C# .Net Core, I can see messages get to the python subscriber, but when I use C# UWP, nothing happens, though the code is exactly the same and I can see Publisher is sending the messages.

The code in python: (Working)

import zmq
import time

def subscribe():
    port = "6789"
    context = zmq.Context()
    socket = context.socket(zmq.SUB)

    socket.connect("tcp://localhost:%s" % port)

    topicfilter = "abcde"
    socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
    while True:
        string = socket.recv()
        print string


subscribe()

The code in .Net Core: (Working)

using System.Threading;
using System.Threading.Tasks;
using NetMQ;
using NetMQ.Sockets;

namespace Examples
{
    static partial class Program
    {
        public static void Main(string[] args)
        {
            Publisher();
        }

        public static void Publisher()
        {
            Task.Run(async () =>
            {
                using (var pubSocket = new PublisherSocket())
                {
                    pubSocket.Bind("tcp://*:6789");

                    for (var i = 0; i < 10; i++)
                    {
                        pubSocket.SendFrame("abcde" + i.ToString());
                        Thread.Sleep(1000);
                    }
                }

            });

        }

    }
}

But the code in UWP (Not working):

using NetMQ;
using NetMQ.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using System;

namespace test_NetMQ_UWP
{

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
            DataContext = this;
        }

        // this event happen when I click on a button in MainPage.xaml
        private void Publisher_Click(object sender, RoutedEventArgs e)
        {
            Task.Run(async () =>
            {
                using (var pubSocket = new PublisherSocket())
                {
                    pubSocket.Bind("tcp://*:6789");

                    for (var i = 0; i < 10; i++)
                    {
                        pubSocket.SendFrame("abcde" + i.ToString());
                        Thread.Sleep(1000);
                    }
                }

            });
        }
    }
}

What am I doing wrong?

Izik
  • 746
  • 1
  • 9
  • 25

1 Answers1

3

It's normal behavior. You're using an IP loopback address for Network communications between a UWP app and a different process (a different UWP app or a desktop app). This is restricted by network isolation.

You could run your server and client on different machine to test. Please see the document How to enable loopback and troubleshoot network isolation (Windows Runtime apps). It has explained this scenario:

Loopback is permitted only for development purposes. Usage by a Windows Runtime app installed outside of Visual Studio is not permitted. Further, a Windows Runtime app can use an IP loopback only as the target address for a client network request. So a Windows Runtime app that uses a DatagramSocket or StreamSocketListener to listen on an IP loopback address is prevented from receiving any incoming packets.

In your case, if you just want to test if the UWP app can send message to your python subscriber successfully. You could run the UWP app on another machine. I used your code to make a UWP app to send message and make a console application as subscriber which is run on a different machine. The console application can receive the message.

Please note that because your UWP app need to access the Network at runtime, you need to enable the Netwrok capabilities(Internet(Client) Internet(Client & Server) Private Networks(Client & Server)) in Package.appxmanifest file.

Xie Steven
  • 8,544
  • 1
  • 9
  • 23