zeromq / netmq

A 100% native C# implementation of ZeroMQ for .NET
Other
2.95k stars 743 forks source link

Communicating with Python issue #1067

Closed masfoobar closed 1 year ago

masfoobar commented 1 year ago

Environment

NetMQ Version:    4.0.1.12
Operating System: Windows
.NET Version:  Currently testing with .NET 6

Python Version: 3.11.4
libzmq version: 4.3.4

Expected behaviour

I am trying to use NetMQ so our C# applications can communicate with Raspberry Pi's. Using Push-Pull sample, I can get a Python program to talk to another Python program. However, I cannot get C# (NetMQ) to talk to the Python program.

Removing Raspberry Pi's for the moment - I am testing push and pull on the same machine.

Actual behaviour

When testing push.py and pull.py -- it works as expected.

push.py

import zmq

def sender():
    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.connect("tcp://localhost:5555")
    socket.send_string("Hello from Windows Python!")

sender()

pull.py

import zmq

def consumer():
    context = zmq.Context()
    socket = context.socket(zmq.PULL)
    socket.bind("tcp://127.0.0.1:5555")

    print("Current libzmq version is %s" % zmq.zmq_version())

    while True:
        input_data = socket.recv()

        print(f"result is: {input_data}")

consumer()

However, when I test my C# project to communicate with pull.py, nothing is received. Here is the C# code (which would communicate with pull.py)

using NetMQ;
using NetMQ.Sockets;
using System.Text;

using (var sender = new PushSocket())
{
    //ForceDotNet.Force();    //<-- found this online.. needed? Makes not difference, currently

    sender.Connect("tcp://127.0.0.1:5555");   //<-- also did "tcp://localhost:5555"

    var str= "Hello from Windows C#";
    sender.SendFrame(str);
}

NetMQConfig.Cleanup();

Steps to reproduce the behaviour

1) Testing the push.py and pull.py (works) Open Powershell (window 1)

python3 pull.py

Open another Powershell (window 2)

python3 push.py

Window 1, everytime you execute the line in Window 2, will spit out "result is: b'Hello Windows Python!'"

2) Testing C# and pull.py (does not work) Open Powershell (window 1)

python3 pull.py

Run C# code (I do this in Visual Studio) Step through the code... nothing gets printed in Window 1 Expecting something like "Hello from Windows C#" to come out, or an error message if in wrong format.

Final notes

Please note I have tried different variations.. sending (from C#) as bytes, as well as trying recv_string or recv_json in python. Does not make any difference.

I don't think I have any confusion with the versions I am using. According to the 'Read the Docs' NetMQ should play nice with ZeroMQ, generally speaking.

Any advice or ideas -- more than welcome. Thank you.

masfoobar commented 1 year ago

Additional note -- the Raspberry Pi's are using Linux. I guess this is the "next step" when I can get the above working on Windows.. on the same machine.

Thanks.

dxdjgl commented 1 year ago

Maybe you should wait before you dispose your PushSocket to give your python code a chance to receive the message. I just extended you example code, and it works nicely. using NetMQ; using NetMQ.Sockets;

public class Issue { static void Main() {

    using (var sender = new PushSocket())
    {
        //ForceDotNet.Force();    //<-- found this online.. needed? Makes not difference, currently

        sender.Connect("tcp://127.0.0.1:5555"); //<-- also did "tcp://localhost:5555"

        var str = "Hello from Windows C#";
        sender.SendFrame(str);
        Thread.Sleep(1000);
    }

    NetMQConfig.Cleanup();
}

}