Ejemplo n.º 1
0
        public static void Run()
        {
            var mre = new ManualResetEvent(false);

            using (var server = new W.Net.Server <W.Net.Client <Customer> >())
            {
                server.ClientConnected += c =>
                {
                    c.MessageReceived += (o, customer) =>
                    {
                        //increase the age and echo the customer
                        customer.Age += 1;
                        customer.Name = customer.Name.ToUpper();
                        o.As <W.Net.Client <Customer> >().Send(customer);
                    };
                };
                server.Start(IPAddress.Parse("127.0.0.1"), 5150);

                using (var client = new W.Net.Client <Customer>())
                {
                    client.MessageReceived += (o, customer) =>
                    {
                        Console.WriteLine("{0} New Age = {1}", customer.Name, customer.Age);
                        Console.WriteLine("Send <Return To Exit>: ");
                    };
                    client.Connected += (o, ep) =>
                    {
                        mre.Set();
                    };
                    client.Disconnected += (s, remoteEndPoint, exception) =>
                    {
                        mre.Set();
                    };
                    client.Socket.ConnectAsync(IPAddress.Parse("127.0.0.1"), 5150).Wait();
                    mre.WaitOne();

                    Console.WriteLine("Send <Return To Exit>: ");
                    while (client.Socket.IsConnected)
                    {
                        var name = Console.ReadLine();
                        if (string.IsNullOrEmpty(name))
                        {
                            break;
                        }
                        var customer = new Customer()
                        {
                            Name = name, Age = new Random().Next(1, 75)
                        };
                        Console.WriteLine("{0} Age = {1}", customer.Name, customer.Age);
                        client.Send(customer);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void Run()
        {
            var mre = new ManualResetEvent(false);

            using (var server = new W.Net.Server <W.Net.Client <string> >())
            {
                server.ClientConnected += client =>
                {
                    Console.WriteLine("Server Connected To Client: " + client.Socket.Name);
                    client.MessageReceived += (c, 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.Client <string>())
                {
                    client.Connected += (c, remoteEndPoint) =>
                    {
                        Console.WriteLine("Client Connected: " + c.Socket.Name);
                    };
                    client.Connected += (c, 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.RawDataReceived += (c, bytes) =>
                    {
                        Console.WriteLine("Client Received Raw: " + bytes?.AsString() ?? "null");
                    };
                    client.MessageReceived += (c, message) =>
                    {
                        Console.WriteLine("Client Received: " + message);
                        Console.Write("Send <Return To Disconnect>: ");
                    };
                    //client.MessageSent += (c, 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();
        }