最近,我使用NetMQ发送或接收服务器和客户端之间的消息。 服务器密码:
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); } }
客户端代码:
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); } }
当我运行两个应用程序中,服务器应用程序,另一个是客户端应用程序。
-
1:客户端发送消息到服务器
-
2:服务器可以接收来自客户机的消息
-
3:服务器发送另一个消息给客户端
-
4:客户端不能接收消息! !
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31559515/viewspace-2221264/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/31559515/viewspace-2221264/