ZeroMQ C#客户端不接收来自C++服务器的消息
ZeroMQ C# client doesnt receive messages from C++ server
我曾尝试对从一台服务器向多个客户端发送消息进行编程。我必须在客户端使用C#,在服务器端使用C++。我以http://zguide.zeromq.org/page:all#toc8对于服务器:
#define within(num) (int) ((float) num * rand () / (RAND_MAX + 1.0))
int main () {
// Prepare our context and publisher
zmq::context_t context (1);
zmq::socket_t publisher (context, ZMQ_PUB);
publisher.bind("tcp://*:5556");
//publisher.bind("ipc://weather.ipc");
// Initialize random number generator
srand ((unsigned) time (NULL));
while (1) {
int zipcode, temperature, relhumidity;
// Get values that will fool the boss
zipcode = within (100000);
temperature = within (215) - 80;
relhumidity = within (50) + 10;
// Send message to all subscribers
zmq::message_t message(20);
_snprintf ((char *) message.data(), 20 ,
"%05d %d %d", zipcode, temperature, relhumidity);
publisher.send(message);
}
return 0;
}
对于客户:
namespace ZMQGuide
{
internal class Program
{
public static void Main(string[] args) {
Console.WriteLine("Collecting updates from weather server…");
// default zipcode is 10001
string zipcode = "10001 "; // the reason for having a space after 10001 is in case of the message would start with 100012 which we are not interested in
if (args.Length > 0)
zipcode = args[1] + " ";
using (var context = new Context(1))
{
using (Socket subscriber = context.Socket(SocketType.SUB))
{
subscriber.Subscribe(zipcode, Encoding.Unicode);
subscriber.Connect("tcp://localhost:5556");
const int updatesToCollect = 100;
int totalTemperature = 0;
for (int updateNumber = 0; updateNumber < updatesToCollect; updateNumber++)
{
string update = subscriber.Recv(Encoding.Unicode);
totalTemperature += Convert.ToInt32(update.Split()[1]);
}
Console.WriteLine("Average temperature for zipcode {0} was {1}F", zipcode, totalTemperature / updatesToCollect);
}
}
}
}
}
他们彼此不交流。在客户端(C++),我评论了ipc交互行,因为在Windows客户端上ipc失败了。在这种情况下,C#-C#、C++-C++交互可以正常工作。我使用clrzmq 2.2.5。
如果有任何帮助,我将不胜感激。
C#客户端正在使用Encoding.Unicode,这是一种2字节的Unicode表示形式(UTF-16)。C++服务器正在使用ASCII。
ZMQ订阅匹配在字节级别工作,不在字符编码之间转换,所以这就是我的问题所在。在C#客户端中切换到Encoding.ASCII或Encoding.UTF8解决了这个问题。
相关文章:
- "unknown ca"自生成的 CA、证书和客户端/服务器
- 当我编译webrtc服务器时,Windows上只支持clang-cl
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 有没有办法知道Tracer是否成功地完全连接到了jaegerclientcpp中的jaeger后端服务器
- 从服务器传输到客户端的消息不会出现
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- OpenSSL TLS服务器-使用客户端证书白名单
- 我正在开发服务器,ip作为参数传递不起作用
- 我可以与 python 服务器而不是 c++ 客户端建立 tcp/ip 套接字吗?
- 提升 Asio TCP 服务器 处理多个客户端
- 如何在 Minecraft 服务器上执行命令
- 调试编译的服务器在数据库打开时崩溃
- C++套接字客户端到 Python 服务器未创建连接
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 将相机数据从服务器实时流式传输到客户端
- 为什么创建服务器后C++ httplib 库阻塞了我的主线程?
- 每个服务器请求的内存预算
- 如何将 Firebase 与基于 Linux 的客户端应用配合使用,以便与服务器进行双向消息通信
- 使用嵌入式猫鼬服务器托管应用程序
- NodeJs 服务器充斥着 UDP 广播,不发送响应