0
我有一個簡單的應用程序發送和接收數據。如何使用ZeroMQ接收數據?
ZSocketExample client = new ZSocketExample("127.0.0.1:5555");
client.send("test");
這是我的客戶端類:
public class ZSocketExample:IDisposable
{
public delegate void ReceiveEventHandler(object sender, SocketEventArgs e);
public event ReceiveEventHandler ReceiveEvent;
private ZmqContext zmqContext;
private ZmqSocket zmqSocket;
private string host;
private bool isRunning;
private bool disposed = false;
public ZSocketExample(string host)
{
try
{
zmqContext = ZmqContext.Create();
zmqSocket = zmqContext.CreateSocket(SocketType.DEALER);
ZHelpers.SetID(zmqSocket, Encoding.UTF8);
zmqSocket.Connect(host);
this.isRunning = true;
zmqSocket.ReceiveReady += new EventHandler<SocketEventArgs>(zmqSocket_ReceiveReady);
zmqSocket.SendReady += new EventHandler<SocketEventArgs>(zmqSocket_SendReady);
Poller poller = new Poller(new List<ZmqSocket> { zmqSocket });
while (isRunning)
{
poller.Poll(TimeSpan.FromSeconds(5));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
void zmqSocket_ReceiveReady(object sender, SocketEventArgs e)
{
Console.WriteLine("Receive Ready");
}
void zmqSocket_SendReady(object sender, SocketEventArgs e)
{
Console.WriteLine("Send Ready");
}
public void send(string msg)
{
zmqSocket.Send(msg, Encoding.UTF8);
if (ReceiveEvent != null)
ReceiveEvent(this, null);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
close();
}
disposed = true;
}
public void close()
{
isRunning = false;
zmqSocket.Linger = TimeSpan.FromSeconds(1);
zmqSocket.Close();
zmqContext.Terminate();
}
}
但不知何故,它亙古不變的發送或接收。有人能告訴我我做錯了什麼嗎?這個例子阻止了主應用程序。我怎樣才能使它不被阻塞?
是否有任何已知的模式創建單個連接併發送/ recvs在線程或異步? – 2014-12-08 00:02:08
與您所熟悉的方式不同,zeromq在如何執行多線程方面有不同的方法,在覈心上,您不會在線程之間共享任何內容並只發送消息。您可以查看指南中的majordomo示例。 – somdoron 2014-12-08 09:52:12
也許我應該切換到NetMQ。你可以告訴我,如果它可能與NetMQ? – 2014-12-08 15:54:12