コード例 #1
0
ファイル: ConnectedClients.cs プロジェクト: Tratos/EasyTcp
        public void TestConnectedClients()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer().Start(port);
            Assert.IsEmpty(server.GetConnectedClients());

            using var client = new EasyTcpClient();
            client.Connect(IPAddress.Loopback, port);
            TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 1);
            Assert.AreEqual(1, server.ConnectedClientsCount);

            using var client2 = new EasyTcpClient();
            client2.Connect(IPAddress.Loopback, port);
            TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 2);
            Assert.AreEqual(2, server.ConnectedClientsCount);

            client.Dispose();
            TestHelper.WaitWhileTrue(() => server.ConnectedClientsCount == 2);
            Assert.AreEqual(1, server.ConnectedClientsCount);

            client2.Dispose();
            TestHelper.WaitWhileTrue(() => server.ConnectedClientsCount == 1);
            Assert.AreEqual(0, server.ConnectedClientsCount);
        }
コード例 #2
0
ファイル: SendAllUtil.cs プロジェクト: Job79/EasyTcp
 /// <summary>
 /// Send data to all connected clients
 /// </summary>
 /// <param name="server"></param>
 /// <param name="dataArray">data to send to connected clients</param>
 public static void SendAll(this EasyTcpServer server, params byte[][] dataArray)
 {
     if (server == null || !server.IsRunning)
     {
         throw new Exception("Could not send data: server not running or null");
     }
     foreach (var client in server.GetConnectedClients())
     {
         client.Protocol.SendMessage(client, dataArray);
     }
 }
コード例 #3
0
 /// <summary>
 /// Execute user command
 /// </summary>
 /// <param name="command">name of action</param>
 /// <param name="args">command arguments</param>
 /// <param name="server"></param>
 /// <param name="selectedClient"></param>
 private void ExecuteCommand(string command, string[] args, EasyTcpServer server, ref EasyTcpClient selectedClient)
 {
     if (command == "s" || command == "select")
     {
         selectedClient = SelectClient(new List <EasyTcpClient>(server.GetConnectedClients()));
     }
     else
     {
         selectedClient?.SendAction(command, args.Any() ? Encoding.UTF8.GetBytes(string.Join(':', args)) : null,
                                    true);
     }
 }
コード例 #4
0
        public void TestIp()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer().Start(port);

            using var client = new EasyTcpClient();
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));

            Assert.AreEqual(IPAddress.Loopback, client.GetIp());
            Assert.AreEqual(IPAddress.Loopback, server.GetConnectedClients().First().GetIp());
        }
コード例 #5
0
        /// <summary>
        /// Send data (byte[][]) to all connected clients
        /// </summary>
        /// <param name="server"></param>
        /// <param name="dataArray">data to send to all connected clients</param>
        /// <exception cref="ArgumentException">invalid server</exception>
        public static void SendAll(this EasyTcpServer server, params byte[][] dataArray)
        {
            if (server == null || !server.IsRunning)
            {
                throw new Exception("Could not send data: Server not running or null");
            }

            var message = SendUtil.CreateMessage(dataArray);

            foreach (var client in server.GetConnectedClients())
            {
                SendUtil.SendMessage(client.BaseSocket, message);
            }
        }
コード例 #6
0
        public void TestClientActionsTrigger()
        {
            ushort port = TestHelper.GetPort();

            using var server = new EasyTcpServer().Start(port);

            using var client      = new EasyTcpActionClient();
            client.OnDataReceive += (sender, message) => Console.WriteLine("123");
            Assert.IsTrue(client.Connect(IPAddress.Loopback, port));

            string data = "test";

            foreach (var c in server.GetConnectedClients())
            {
                var reply = c.SendActionAndGetReply("ECHO", data);
                Assert.AreEqual(data, reply.ToString());
            }
        }
コード例 #7
0
ファイル: UtilsExamples.cs プロジェクト: zhouzu/EasyTcp
        public static async Task Start()
        {
            using var server = new EasyTcpServer();
            var socket = server.BaseSocket; // Get baseSocket, null when server is disposed

            // Start server on given port (default address: "0.0.0.0")
            server.Start(Port);

            // Start server on given port and address
            server.Start("0.0.0.0", Port);
            server.Start(IPAddress.Any, Port);

            // Send message to all connected clients
            server.SendAll("Hello clients!");

            // Get copy of list with connected clients
            var connectedClientsList = server.GetConnectedClients();
            var connectedSocketsList = server.GetConnectedSockets();

            // Get amount of connected clients
            var connectedClientsCount = server.ConnectedClientsCount;

            server.OnDataReceive += (sender, message) =>
            {
                // Get client
                var client = message.Client;

                // Get endpoint or ip address of client
                var endPoint = client.GetEndPoint();
                var ip       = client.GetIp();

                // Use all the other clientUtil on client
                client.Send("Hello from server!");

                // Determines whether message is compressed
                var isCompressed = message.IsCompressed();

                /* Decompress message,
                 * ignored when message is not compressed
                 */
                var data = message.Decompress();
            };
        }