简体   繁体   English

如何使用 ZeroMQ 从 C# 客户端向 C++ 服务器发送消息

[英]How to send a message from C# client to C++ server using ZeroMQ

I am making an application that works in three different parts.我正在制作一个适用于三个不同部分的应用程序。 One server running in C ++ and two clients running in C # and Python.一台服务器运行在 C ++ 和两个客户端运行在 C # 和 Python。 The client connects to the server and sends a message.客户端连接到服务器并发送消息。

To do this I have opted for ZeroMQ.为此,我选择了 ZeroMQ。

Because this is a POC, the code is simple.因为这是一个 POC,所以代码很简单。

Server Code (C++) (cppzmq)服务器代码 (C++) (cppzmq)

zmq::context_t context{ 1 };
zmq::socket_t socket{ context, zmq::socket_type::rep };
socket.bind("tcp://*:5555");
zmq::message_t request;
socket.recv(request, zmq::recv_flags::none);

Python Client (pyzm) Python 客户端(pyzm)

context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket.send(b"Hello")

C# client (NetMQ) C# 客户端(NetMQ)

using(var socket =  new RequestSocket())
{
    socket.Connect("tcp://localhost:5555");
    byte[] msg = Encoding.ASCII.GetBytes("HEllo");
    socket.SendFrame(msg);
 }

The connection between the Python client and the server is successful and the server receives the message sent by the client. Python客户端与服务端连接成功,服务端收到客户端发送的消息。 But the connection between C # client and server is not correct.但是C#客户端和服务器之间的连接不正确。 On the C # side it is assumed that you have successfully connected and transmitted the message, but the server does not reflect any changes (as if you did not receive a call).在 C # 端,假设您已成功连接并发送消息,但服务器没有反映任何更改(就像您没有接到电话一样)。

What can be the cause?可能是什么原因?

for c# I used NetMQMessage.对于 c#,我使用了 NetMQMessage。 Does this work better for you?这对你更有效吗? I use other options (no linger and timeout)我使用其他选项(没有逗留和超时)

using (var client = new RequestSocket()) {
  
   client.Connect("tcp://192.168.1.216:8000");

   NetMQMessage msg = new NetMQMessage();
   msg.Append("my request string buffer", Encoding.UTF8);
   client.SendFrame(msg.ToString());

   var res_bytes = client.ReceiveFrameBytes();
   // add some function to transform bytes to string

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM