[英]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解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.