public static void Execute() { Console.WriteLine("Executing Listener test"); const string inprocAddress = "tcp://127.0.0.1:6522"; const string unusedAddress = "tcp://127.0.0.1:6521"; byte[] buffer1; byte[] buffer2; var clientThread = new Thread( () => { var req1 = NN.Socket(Domain.SP, Protocol.REQ); NN.Connect(req1, unusedAddress); var req = NN.Socket(Domain.SP, Protocol.REQ); NN.Connect(req, inprocAddress); Thread.Sleep(TimeSpan.FromSeconds(3)); NN.Send(req, BitConverter.GetBytes((int)42), SendRecvFlags.NONE); NN.Recv(req, out buffer1, SendRecvFlags.NONE); Debug.Assert(BitConverter.ToInt32(buffer1, 0) == 77); Console.WriteLine("Response: " + BitConverter.ToInt32(buffer1, 0)); }); clientThread.Start(); var unused = NN.Socket(Domain.SP, Protocol.REP); NN.Bind(unused, unusedAddress); var rep = NN.Socket(Domain.SP, Protocol.REP); NN.Bind(rep, inprocAddress); var listener = new NanomsgListener(); listener.AddSocket(unused); listener.AddSocket(rep); listener.ReceivedMessage += delegate(int s) { NN.Recv(s, out buffer2, SendRecvFlags.NONE); Console.WriteLine("Message: " + BitConverter.ToInt32(buffer2, 0)); NN.Send(s, BitConverter.GetBytes((int)77), SendRecvFlags.NONE); }; while (true) { listener.Listen(TimeSpan.FromMinutes(30)); } }
public static void Execute(string[] args) { const string socketAddress = "tcp://127.0.0.1:5088"; if (args[1] == "client") { using (var req = new RequestSocket()) { req.Connect(socketAddress); req.Send(Encoding.UTF8.GetBytes("hello from client")); Console.WriteLine("Message from SERVER: " + Encoding.UTF8.GetString(req.Receive())); Console.WriteLine("CLIENT finished"); } } else if (args[1] == "server") { using (var rep = new ReplySocket()) { rep.Bind(socketAddress); var listener = new NanomsgListener(); listener.AddSocket(rep); listener.ReceivedMessage += socketId => { Console.WriteLine("Message from CLIENT: " + Encoding.UTF8.GetString(rep.Receive())); rep.Send(Encoding.UTF8.GetBytes("hello from server")); Console.WriteLine("SERVER Finished"); Environment.Exit(0); }; listener.Listen(null); } } else { Console.WriteLine("Unknown argument: " + args[1]); } }
/// <summary> /// A simple example showing use of NNanomsg.Socket /// For further example usage, check out the Test project. /// </summary> static void Main(string[] args) { const string socketAddress = "tcp://127.0.0.1:5088"; if (args[0] == "client") { using (var req = new RequestSocket()) { req.Connect(socketAddress); using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms)) { sw.Write("hello from client"); } req.Send(ms.ToArray()); } using (var ms = new MemoryStream(req.Receive())) using (var sr = new StreamReader(ms)) { Console.WriteLine("Message from SERVER: " + sr.ReadToEnd()); } Console.WriteLine("CLIENT finished"); } } else if (args[0] == "server") { using (var rep = new ReplySocket()) { rep.Bind(socketAddress); var listener = new NanomsgListener(); listener.AddSocket(rep); listener.ReceivedMessage += socketId => { using (var ms = new MemoryStream(rep.Receive())) using (var sr = new StreamReader(ms)) { Console.WriteLine("Message from CLIENT: " + sr.ReadToEnd()); } using (var ms = new MemoryStream()) { using (var sw = new StreamWriter(ms)) { sw.Write("hello from server"); } rep.Send(ms.ToArray()); } Console.WriteLine("SERVER Finished"); Environment.Exit(0); }; listener.Listen(null); } } else { Console.WriteLine("Unknown argument: " + args[0]); } }
public static string Request(string input) { // prepare Socket var nanoSock = new NanomsgSocket(Domain.SP, Protocol.REQ); string result = null; bool messageReceived = false; // prepare Listener var listener = new NanomsgListener(); listener.ReceivedMessage += (sockedId) => { using (NanomsgReadStream inStream = nanoSock.ReceiveStream()) using (var unpacker = Unpacker.Create(inStream)) { string output; if (!unpacker.ReadString(out output)) throw new Exception("REP invalid"); result = output; } messageReceived = true; }; // Connect and Send Request listener.AddSocket(nanoSock); nanoSock.Connect("tcp://127.0.0.1:" + reqPort); using (NanomsgWriteStream outStream = nanoSock.CreateSendStream()) using(var packer = Packer.Create(outStream)) { packer.PackString(input); nanoSock.SendStream(outStream); } var start = DateTime.Now; while (!messageReceived && start + TimeSpan.FromSeconds(30) > DateTime.Now) { listener.Listen(TimeSpan.FromMilliseconds(250)); } listener.RemoveSocket(nanoSock); nanoSock.Dispose(); if (!messageReceived) { throw new Exception("REQ timed out"); } return result; }
public static void RunWorker(CancellationTokenSource cancellor, int workerId) { var serverThread = new Thread(() => { var nanoSock = new NanomsgSocket(Domain.SP, Protocol.REP); var nanoListener = new NanomsgListener(); nanoListener.ReceivedMessage += (socketId) => { string input; using (NanomsgReadStream inStream = nanoSock.ReceiveStream()) using (var unpacker = Unpacker.Create(inStream)) { if (!unpacker.ReadString(out input)) throw new Exception("REQ invalid"); Console.WriteLine(input); } using (NanomsgWriteStream outStream = nanoSock.CreateSendStream()) using (var packer = Packer.Create(outStream)) { packer.PackString("Hello " + input); nanoSock.SendStream(outStream); } }; nanoListener.AddSocket(nanoSock); nanoSock.Connect("tcp://127.0.0.1:" + repPort); while (!cancellor.IsCancellationRequested) { nanoListener.Listen(TimeSpan.FromMilliseconds(250)); } nanoListener.RemoveSocket(nanoSock); nanoSock.Dispose(); }); serverThread.Start(); serverThread.Join(TimeSpan.FromMilliseconds(40)); }