ZmqReqRepTest.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System.Threading.Tasks;
  2. using Log;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using NetMQ;
  5. namespace ZmqTest
  6. {
  7. [TestClass]
  8. public class ZmqReqRepTest
  9. {
  10. const string address = "tcp://127.0.0.1:5001";
  11. [TestMethod]
  12. public void TestMethod()
  13. {
  14. var task1 = Task.Factory.StartNew(Server);
  15. var task2 = Task.Factory.StartNew(Client);
  16. Task.WaitAll(task1, task2);
  17. }
  18. private static void Client()
  19. {
  20. using (var context = NetMQContext.Create())
  21. {
  22. using (var req = context.CreateRequestSocket())
  23. {
  24. var poller = new Poller();
  25. req.Connect(address);
  26. req.ReceiveReady += (sender, args) =>
  27. {
  28. bool hasMore;
  29. string msg = args.Socket.ReceiveString(true, out hasMore);
  30. Logger.Debug(string.Format("req: {0}", msg));
  31. poller.Stop();
  32. };
  33. poller.AddSocket(req);
  34. poller.Start();
  35. }
  36. }
  37. }
  38. private static void Server()
  39. {
  40. using (var context = NetMQContext.Create())
  41. {
  42. using (var rep = context.CreateResponseSocket())
  43. {
  44. var poller = new Poller();
  45. poller.AddSocket(rep);
  46. rep.Bind(address);
  47. rep.ReceiveReady += (sender, args) =>
  48. {
  49. bool hasMore;
  50. string msg = args.Socket.ReceiveString(true, out hasMore);
  51. Logger.Debug(string.Format("rep: {0}", msg));
  52. args.Socket.Send(msg, true);
  53. poller.Stop();
  54. };
  55. poller.Start();
  56. }
  57. }
  58. }
  59. }
  60. }