Recently, I use the NetMQ to send or receive message between server and client. Server codes like:
void Main()
{
CreatePullAndPushSocket();
Task.Factory.StartNew(()=> {
while (true)
{
Thread.Sleep(1);
if (Pull != null)
{
var message = Pull.ReceiveFrameString();
}
}
});
}
PullSocket Pull;
PushSocket Push;
private void CreatePullAndPushSocket()
{
Pull = new PullSocket("tcp://ip1:port1");
Push = new PushSocket("tcp://ip2:port2");
}
public void SendMessageToClient(string message)
{
if (Push != null)
{
Push.SendFrame(message);
}
}
The client codes like:
void Main()
{
new Thread(()=> {
while (true)
{
Thread.Sleep(1);
if (Pull != null)
{
var message = Pull.ReceiveFrameString();
}
}
}).Start();
}
PullSocket Pull;
PushSocket Push;
private void CreatePullAndPushSocket()
{
Pull = new PullSocket("tcp://ip2:port2");
Push = new PushSocket("tcp://ip1:port1");
}
public void SendMessageToClient(string message)
{
if (Push != null)
{
Push.SendFrame(message);
}
}
When I run two application, which is server app, another is client app.
- 1:Client send a message to Server
- 2:Server can receive the message from the client
- 3:Server send another message to client
- 4:The client cann't receive the message!!!
So strange, I have followed the guidance https://netmq.readthedocs.io/en/latest/push-pull/!