示例#1
0
 static void Node0(string url)
 {
     using (var s = new ReplySocket())
     {
         s.Bind(url);
         Console.WriteLine("NODE0: RECEIVED: \"" + Encoding.UTF8.GetString(s.Receive()) + "\"");
         const string sendText = "Goodbye.";
         Console.WriteLine("NODE0: SENDING: \"" + sendText + "\"");
         s.Send(Encoding.UTF8.GetBytes(sendText));
     }
 }
        void Download()
        {
            ReplySocket sockClientListner;
            try
            {

                sockClientListner = new ReplySocket();

                sockClientListner.Bind("tcp://" +global.Instance.RecClientConIp+ ":" +global.Instance.ClientPort_Rec);

                //SubscribeSocket subscriber = new SubscribeSocket();
                //subscriber.Connect("tcp://" +global.Instance.ClientConIp+ ":" +global.Instance.ClientPort_Rec);
                Console.WriteLine("NANOMQ UDP_Reciever Start DataAddress: " + "tcp://" + global.Instance.RecClientConIp+ ":" +global.Instance.ClientPort_Rec);
                //subscriber.Subscribe(BitConverter.GetBytes(Convert.ToInt64(global.Instance.Client1)));
                //subscriber.Subscribe(BitConverter.GetBytes(Convert.ToInt64(global.Instance.Client2)));
                int cnt=0;
                while (true)
                {
                    //Thread.Sleep(10000);
                    //var buffer = subscriber.Receive();
                    Console.WriteLine("cnt :\t"+cnt);

                    var buffer = sockClientListner.Receive();
                    cnt++;
                    Console.WriteLine("Recieved :"+ Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));

                    string msg=Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray());

                    if("" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()) == "upload")
                    {

                        //Console.WriteLine("2" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));
                        Program.Instance.send(BitConverter.ToInt64(buffer,0),msg);

                    }

                    if("" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()) == "recieved")
                    {

                        //Console.WriteLine("2" + Encoding.ASCII.GetString(buffer.Skip(8).Take(buffer.Length).ToArray()));
                        Program.Instance.send(BitConverter.ToInt64(buffer,0),msg);

                    }
                }
                Console.ReadLine();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
示例#3
0
        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]);
            }
        }
示例#4
0
        /// <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]);
            }
        }