我有一个发送和接收数据的简单应用程序。
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();
}
}
但不知何故它不发送或接收。谁能告诉我,我做错了什么?这个例子阻止了主要的应用程序。我怎么能让它不阻塞?
答案 0 :(得分:3)
ZeroMQ套接字不是线程安全的,你不能在不使用某种同步的情况下从多个线程使用它。
在您的示例中,您调用设置变量的close,然后立即关闭套接字,这是错误的,当您离开while循环时应该关闭套接字。
关于接收/发送就绪,您很少需要注册发送就绪,发送就绪会让您知道何时可以发送消息,如果您已连接,则在经销商插座中您始终可以发送(除非高水位)已达到)。
当准备好接收消息时将调用Receive Ready,如果其他方向您发送消息,则将调用receive ready。
对于最后一部分,阻塞,你需要一个专用线程来处理zeromq套接字,你可以有一个线程使用轮询器处理多个套接字。