Пример #1
0
 // Client
 private void Client_Connect(object sender, RoutedEventArgs e)
 {
     client = new CavemanTcpClient(ClientConnectIP.Text + ":11500");
     client.Events.ClientConnected    += Events_ClientConnected;
     client.Events.ClientDisconnected += Events_ClientDisconnected;
     client.Connect(5000);
 }
Пример #2
0
        // Public
        public void StartTransfer(List <DirectoryItem> itemsToSend, Device device)
        {
            if (device == null)
            {
                return;
            }

            if (IsBusy)
            {
                return;
            }


            // Copy list to prevent changes while sending
            items = itemsToSend.Select(item => (DirectoryItem)item.Clone()).ToList();


            // Create connection
            client       = new CavemanTcpClient(device.IPAddress.ToString(), port);
            serverDevice = device;

            client.Events.ClientConnected    += Events_ClientConnected;
            client.Events.ClientDisconnected += Events_ClientDisconnected;

            client.Keepalive.EnableTcpKeepAlives    = true;
            client.Keepalive.TcpKeepAliveInterval   = 5;
            client.Keepalive.TcpKeepAliveTime       = 5;
            client.Keepalive.TcpKeepAliveRetryCount = 5;

            client.Connect(Settings.SettingsData.MaxNetworkPingMs);

            // TODO Add timeout
        }
Пример #3
0
        static void Main(string[] args)
        {
            InitializeClient();
            Console.WriteLine("Connecting to tcp://127.0.0.1:8000");
            _Client.Connect(10);

            while (_RunForever)
            {
                Console.Write("Command [? for help]: ");
                string userInput = Console.ReadLine();
                if (String.IsNullOrEmpty(userInput))
                {
                    continue;
                }

                if (userInput.Equals("?"))
                {
                    Console.WriteLine("-- Available Commands --");
                    Console.WriteLine("");
                    Console.WriteLine("   cls                  Clear the screen");
                    Console.WriteLine("   q                    Quit the program");
                    Console.WriteLine("   send [data]          Send data to the server");
                    Console.WriteLine("   sendt [ms] [data]    Send data to the server with specified timeout");
                    Console.WriteLine("   read [count]         Read [count] bytes from the server");
                    Console.WriteLine("   readt [ms] [count]   Read [count] bytes from the server with specified timeout");
                    Console.WriteLine("   dispose              Dispose of the client");
                    Console.WriteLine("   start                Start the client (connected: " + (_Client != null ? _Client.IsConnected.ToString() : "false") + ")");
                    Console.WriteLine("   stats                Retrieve statistics");
                    Console.WriteLine("");
                    continue;
                }

                if (userInput.Equals("c") || userInput.Equals("cls"))
                {
                    Console.Clear();
                    continue;
                }

                if (userInput.Equals("q"))
                {
                    _RunForever = false;
                    break;
                }

                if (userInput.StartsWith("send "))
                {
                    string[] parts = userInput.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                    string   data  = parts[1];

                    WriteResult wr = _Client.SendAsync(data).Result;
                    if (wr.Status == WriteResultStatus.Success)
                    {
                        Console.WriteLine("Success");
                    }
                    else
                    {
                        Console.WriteLine("Non-success status: " + wr.Status);
                    }
                }

                if (userInput.StartsWith("sendt "))
                {
                    string[] parts     = userInput.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
                    int      timeoutMs = Convert.ToInt32(parts[1]);
                    string   data      = parts[2];

                    WriteResult wr = _Client.SendWithTimeoutAsync(timeoutMs, data).Result;
                    if (wr.Status == WriteResultStatus.Success)
                    {
                        Console.WriteLine("Success");
                    }
                    else
                    {
                        Console.WriteLine("Non-success status: " + wr.Status);
                    }
                }

                if (userInput.StartsWith("read "))
                {
                    string[] parts = userInput.Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);
                    int      count = Convert.ToInt32(parts[1]);

                    ReadResult rr = _Client.ReadAsync(count).Result;
                    if (rr.Status == ReadResultStatus.Success)
                    {
                        Console.WriteLine("Retrieved " + rr.BytesRead + " bytes: " + Encoding.UTF8.GetString(rr.Data));
                    }
                    else
                    {
                        Console.WriteLine("Non-success status: " + rr.Status.ToString());
                    }
                }

                if (userInput.StartsWith("readt "))
                {
                    string[] parts     = userInput.Split(new char[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
                    int      timeoutMs = Convert.ToInt32(parts[1]);
                    int      count     = Convert.ToInt32(parts[2]);

                    ReadResult rr = _Client.ReadWithTimeoutAsync(timeoutMs, count).Result;
                    if (rr.Status == ReadResultStatus.Success)
                    {
                        Console.WriteLine("Retrieved " + rr.BytesRead + " bytes: " + Encoding.UTF8.GetString(rr.Data));
                    }
                    else
                    {
                        Console.WriteLine("Non-success status: " + rr.Status.ToString());
                    }
                }

                if (userInput.Equals("dispose"))
                {
                    _Client.Dispose();
                }

                if (userInput.Equals("start"))
                {
                    InitializeClient();
                    _Client.Connect(10);
                }

                if (userInput.Equals("stats"))
                {
                    Console.WriteLine(_Client.Statistics);
                }
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            _Token = _TokenSource.Token;

            Task.Run(() => StartEchoServer(), _Token);

            _Client = new CavemanTcpClient("127.0.0.1", 9000, false, null, null);
            _Client.Events.ClientConnected += (s, e) =>
            {
                Console.WriteLine("[Client] connected");
            };

            _Client.Events.ClientDisconnected += (s, e) =>
            {
                Console.WriteLine("[Client] disconnected");
            };

            _Client.Logger = Console.WriteLine;

            while (_RunForever)
            {
                Console.Write("Command [? for help]: ");
                string userInput = Console.ReadLine();
                if (String.IsNullOrEmpty(userInput))
                {
                    continue;
                }

                switch (userInput)
                {
                case "?":
                    Console.WriteLine("Available commands:");
                    Console.WriteLine("q         quit");
                    Console.WriteLine("cls       clear the screen");
                    Console.WriteLine("connect   connect to server");
                    Console.WriteLine("send      send clientecho to the server");
                    Console.WriteLine("read      read data from the server");
                    Console.WriteLine("");
                    break;

                case "q":
                    _RunForever = false;
                    break;

                case "cls":
                    Console.Clear();
                    break;

                case "connect":
                    _Client.Connect(5000);
                    break;

                case "send":
                    _Client.Send("clientecho");
                    break;

                case "read":
                    ReadResult rr = _Client.Read(10);
                    if (rr.Status == ReadResultStatus.Success)
                    {
                        Console.WriteLine("[Client] read 10 bytes: " + Encoding.UTF8.GetString(rr.Data));
                    }
                    else
                    {
                        Console.WriteLine("*** [Client] read status: " + rr.Status.ToString());
                    }
                    break;
                }
            }
        }