예제 #1
0
        private static void SelfHost()
        {
            ManualResetEventSlim mre        = new ManualResetEventSlim(true);
            IPEndPoint           ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2213);

            using (var server = new W.Net.SecureServer <W.Net.SecureClient <string> >())
            {
                server.ClientConnected += client =>
                {
                    client.As <W.Net.SecureClient <string> >().MessageReceived += (o, message) =>
                    {
                        Console.WriteLine(message);
                        mre.Set();
                    };
                };
                server.Start(ipEndPoint.Address, ipEndPoint.Port);

                //To verify this method, an external server must be listening
                using (var logger = new W.Net.SecureStringClientLogger(ipEndPoint))
                {
                    var r = new Random();
                    while (true)
                    {
                        mre.Wait();
                        Console.Write("Send <Return to Exit>:");
                        var msg = Console.ReadLine().Trim();
                        if (string.IsNullOrEmpty(msg))
                        {
                            break;
                        }
                        mre.Reset();

                        switch (r.Next(0, 4))
                        {
                        case 0:
                            W.Logging.Log.e(msg);
                            break;

                        case 1:
                            W.Logging.Log.w(msg);
                            break;

                        case 2:
                            W.Logging.Log.i(msg);
                            break;

                        case 3:
                            W.Logging.Log.v(msg);
                            break;
                        }
                    }
                    Console.WriteLine("Complete");
                }
            }
        }
        public static void Run()
        {
            var mre = new ManualResetEvent(false);

            using (var server = new W.Net.SecureServer <W.Net.SecureClient <string> >())
            {
                server.ClientConnected += client =>
                {
                    client.Socket.UseCompression = true;
                    Console.WriteLine("Client Connected To Server");
                    client.MessageReceived += (proxy, message) =>
                    {
                        if (!string.IsNullOrEmpty(message.As <string>()))
                        {
                            Console.WriteLine("Server Echo: " + message);
                            proxy.As <W.Net.SecureClient <string> >().Send(message.As <string>().ToUpper());
                        }
                    };
                };
                server.ClientDisconnected += (client, remoteEndPoint, exception) =>
                {
                    if (exception != null)
                    {
                        Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString() + " - " + exception.Message);
                    }
                    else
                    {
                        Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString());
                    }
                };
                server.Start(IPAddress.Parse("127.0.0.1"), 5150);

                using (var secureClient = new W.Net.SecureClient <string>())
                {
                    secureClient.Connected += (ssc, remoteEndPoint) =>
                    {
                        Console.WriteLine("Client Connected: " + remoteEndPoint?.ToString());
                    };
                    secureClient.Connected += (scc, ep) =>
                    {
                        mre.Set();
                    };
                    secureClient.Disconnected += (scc, remoteEndPoint, exception) =>
                    {
                        if (exception != null)
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString() + " - " + exception.Message);
                        }
                        else
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString());
                        }
                        mre.Set();
                    };
                    secureClient.MessageReceived += (s, message) =>
                    {
                        Console.WriteLine("Client Received: " + message);
                        Console.Write("Send <Return To Exit>: ");
                    };
                    //secureClient.MessageSent += (c, message) =>
                    //{
                    //    Console.WriteLine("Client Message Sent");
                    //};
                    secureClient.Socket.UseCompression = true;
                    secureClient.Socket.ConnectAsync("127.0.0.1", 5150).Wait();
                    mre.WaitOne();

                    Console.Write("Send <Return To Exit>: ");
                    while (secureClient.Socket.IsConnected)
                    {
                        var message = Console.ReadLine();
                        if (string.IsNullOrEmpty(message))
                        {
                            break;
                        }
                        secureClient.Send(message);
                    }
                }
            }
        }
예제 #3
0
        public static async void Run()
        {
            var mre = new ManualResetEvent(false);

            using (var server = new W.Net.SecureServer <W.Net.SecureClient <string> >())
            {
                server.ClientConnected += client =>
                {
                    Console.WriteLine("Server Connected To Client: " + client.As <W.Net.SecureClient <string> >().Socket.Name);
                    client.MessageReceived += (proxy, message) =>
                    {
                        if (!string.IsNullOrEmpty(message.As <string>()))
                        {
                            Console.WriteLine("Server Echo: " + message);
                            proxy.As <W.Net.SecureClient <string> >().Send(message.As <string>().ToUpper());
                        }
                    };
                };
                server.ClientDisconnected += (client, remoteEndPoint, exception) =>
                {
                    if (exception != null)
                    {
                        Console.WriteLine("Client Disconnected: " + client.As <W.Net.SecureClient <string> >().Socket.Name + " - " + exception.Message);
                    }
                    else
                    {
                        Console.WriteLine("Client Disconnected: " + client.As <W.Net.SecureClient <string> >().Socket.Name);
                    }

                    Console.WriteLine("Server Disconnected From: " + remoteEndPoint?.ToString());
                };
                server.Start(IPAddress.Parse("127.0.0.1"), 5150);

                using (var client = new W.Net.SecureClient <string>())
                {
                    client.Connected += (ssc, remoteEndPoint) =>
                    {
                        Console.WriteLine("Client Connected: " + ssc.As <W.Net.SecureClient <string> >().Socket.Name);
                    };
                    client.Connected += (ssc, ep) =>
                    {
                        mre.Set();
                    };
                    client.Disconnected += (ssc, remoteEndPoint, exception) =>
                    {
                        if (exception != null)
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString() + " - " + exception.Message);
                        }
                        else
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString());
                        }
                        mre.Set();
                    };
                    client.MessageReceived += (s, message) =>
                    {
                        Console.WriteLine("Client Received: " + message);
                        Console.Write("Send <Return To Disconnect>: ");
                    };

                    //we can't decrypt the data because we don't have the private key (so we need a better way to track sent data)
                    //client.MessageSent += (c, message) =>
                    //{
                    //    Console.WriteLine("Client Message Sent");
                    //};

                    for (int t = 0; t < 20; t++)
                    {
                        var connected = client.Socket.ConnectAsync("127.0.0.1", 5150).Result;
                        Console.WriteLine("Connected = {0}", connected);
                        mre.WaitOne();
                        client.Socket.Disconnect();
                        //Console.WriteLine("Disconnected");
                    }

                    Console.Write("Send <Return To Disconnect>: ");
                    while (client.Socket.IsConnected)
                    {
                        var message = Console.ReadLine();
                        if (string.IsNullOrEmpty(message))
                        {
                            break;
                        }
                        client.Send(message);
                    }
                }
            }
            Console.WriteLine("Press Any Key To Return");
            Console.ReadKey(true);
        }
예제 #4
0
        public static void Run()
        {
            var mre = new ManualResetEvent(false);

            using (var server = new W.Net.SecureServer <W.Net.SecureClient <string> >())
            {
                server.ClientConnected += client =>
                {
                    Console.WriteLine("Server Connected To Client: " + client.Socket.Name);
                    client.DataReceived += (s, message) =>
                    {
                        Console.WriteLine("Server Received Data: " + message?.AsString() ?? "null");
                    };
                    client.MessageReceived += (c, message) =>
                    {
                        if (!string.IsNullOrEmpty(message))
                        {
                            Console.WriteLine("Server Echo: " + message);
                            c.Send(message.As <string>().ToUpper());
                        }
                    };
                };
                server.ClientDisconnected += (client, remoteEndPoint, exception) =>
                {
                    if (exception != null)
                    {
                        Console.WriteLine("Server Disconnected: " + client.Socket.Name + " - " + exception.Message);
                    }
                    else
                    {
                        Console.WriteLine("Server Disconnected: " + client.Socket.Name);
                    }

                    Console.WriteLine("Server Disconnected From: " + remoteEndPoint?.ToString());
                };
                server.Start(IPAddress.Parse("127.0.0.1"), 5150);

                using (var client = new W.Net.SecureClient <string>())
                {
                    client.Connected += (c, remoteEndPoint) =>
                    {
                        Console.WriteLine("Client Connected: " + c.Socket.Name);
                    };
                    client.Connected += (ssc, ep) =>
                    {
                        mre.Set();
                    };
                    client.Disconnected += (c, remoteEndPoint, exception) =>
                    {
                        if (exception != null)
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString() + " - " + exception.Message);
                        }
                        else
                        {
                            Console.WriteLine("Client Disconnected: " + remoteEndPoint?.ToString());
                        }
                        mre.Set();
                    };
                    client.DataReceived += (s, message) =>
                    {
                        Console.WriteLine("Client Received Data: " + message.AsString());
                        //Console.Write("Send <Return To Disconnect>: ");
                    };
                    client.MessageReceived += (c, message) =>
                    {
                        Console.WriteLine("Client Received Message: " + message);
                        Console.Write("Send <Return To Disconnect>: ");
                    };
                    //client.DataSent += (s, m) =>
                    //{
                    //    Console.WriteLine("Client Message Sent");
                    //};

                    client.Socket.ConnectAsync(IPAddress.Parse("127.0.0.1"), 5150).Wait();
                    mre.WaitOne();

                    Console.Write("Send <Return To Disconnect>: ");
                    while (client.Socket.IsConnected)
                    {
                        var message = Console.ReadLine();
                        if (string.IsNullOrEmpty(message))
                        {
                            break;
                        }
                        client.Send(message);
                    }
                }
            }
            Console.WriteLine("Press Any Key To Return");
            Console.ReadKey();
        }
예제 #5
0
        public void TestSecureStringClientLogger()
        {
            var received = 0;
            var numberOfMessagesToSend = 10;
            var mreQuit = new System.Threading.ManualResetEvent(false);


            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.2.12"), 2213);

            using (var server = new W.Net.SecureServer <W.Net.SecureClient <string> >())
            {
                server.ClientConnected += client =>
                {
                    client.MessageReceived += (c, m) =>
                    {
                        received += 1;
                        Console.WriteLine("Received Message " + received.ToString() + ": " + m);
                        if (received == numberOfMessagesToSend)
                        {
                            mreQuit.Set();
                        }
                    };
                    //client.ConnectionSecured += client2 =>
                    //{
                    //    client.As<W.Net.SecureStringClient>().MessageReceived += (o, message) =>
                    //    {
                    //        received += 1;
                    //        Console.WriteLine("Received Message " + received.ToString() + ": " + message);
                    //        if (received == numberOfMessagesToSend)
                    //            mreQuit.Set();
                    //    };
                    //};
                };
                server.Start(ipEndPoint.Address, ipEndPoint.Port);
                Console.WriteLine("Server Started");

                //To verify this method, an external server must be listening
                using (var logger = new W.Net.SecureStringClientLogger(ipEndPoint))
                {
                    var r = new Random();
                    for (int t = 1; t <= numberOfMessagesToSend; t++)
                    {
                        var msg = "Test Log Message: " + t.ToString();
                        switch (r.Next(0, 4))
                        {
                        case 0:
                            W.Logging.Log.e(msg);
                            break;

                        case 1:
                            W.Logging.Log.w(msg);
                            break;

                        case 2:
                            W.Logging.Log.i(msg);
                            break;

                        case 3:
                            W.Logging.Log.v(msg);
                            break;
                        }
                    }
                    if (!mreQuit.WaitOne(10000))
                    {
                        Console.WriteLine("Failed To Receive All Messages");
                    }
                    Console.WriteLine("Completed Logging");
                }
                Console.WriteLine("Complete");
            }
        }