예제 #1
0
        /// <summary>
        /// Send data (byte[][]) to remote host. Then wait and return the reply
        /// </summary>
        /// <param name="client"></param>
        /// <param name="data">data to send to remote host</param>
        /// <param name="timeout">maximum time to wait for a reply, if time expired this function returns null</param>
        /// <returns>received reply</returns>
        public static async Task <Message> SendAndGetReplyAsync(this EasyTcpClient client, TimeSpan?timeout = null,
                                                                params byte[][] data)
        {
            var receive = client.ReceiveAsync(timeout ?? TimeSpan.FromMilliseconds(DefaultTimeout));

            client.Send(data);
            return(await receive);
        }
예제 #2
0
        public async Task TestReceive()
        {
            ushort port = TestHelper.GetPort();

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

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

            var receivedData = client.ReceiveAsync();

            TestHelper.WaitWhileFalse(() => server.ConnectedClientsCount == 1);
            server.SendAll("Data");
            Assert.AreEqual("Data", (await receivedData).ToString());
        }
예제 #3
0
        public static async Task Connect()
        {
            using var client = new EasyTcpClient();
            var socket = client.BaseSocket; // Get baseSocket, null when client is disposed/disconnected

            // Connect to remote host with IP and Port
            if (!client.Connect("127.0.0.1", Port))
            {
                return;
            }
            if (!client.Connect(IPAddress.Loopback, Port))
            {
                return;
            }

            // Connect to remote host with IP, Port and timeout (Default: 5 seconds)
            if (!client.Connect("127.0.0.1", Port, TimeSpan.FromSeconds(10)))
            {
                return;
            }

            // Async connect to remote host with IP and Port
            if (!await client.ConnectAsync("127.0.0.1", Port))
            {
                return;
            }

            // Async send data to server
            client.Send("Hello server!");
            client.Send(1);
            client.Send(1.000);
            client.Send(true);
            client.Send("Hello server!", true); // Async send compressed data

            /* Send data to server and get reply
             * OnDataReceive is not triggered when receiving a reply
             *
             * ! SendAndGetReply does not work in OnDataReceive
             * ! first call client.Protocol.EnsureDataReceiverIsRunning(client) when using in OnDataReceive
             */
            var reply = client.SendAndGetReply("data");

            // Send data to server and get reply with a specified timeout (Default: 5 seconds)
            // SendAndGetReply returns null when no message is received within given time frame
            var replyWithTimeout = client.SendAndGetReply("data", TimeSpan.FromSeconds(5));

            // Send data to server and get reply async
            var reply2 = await client.SendAndGetReplyAsync("data");

            /* Get next received data as variable
             * OnDataReceive is not triggered
             */
            var receivedData = await client.ReceiveAsync();

            // Use timeout (Default: infinite)
            // Returns null when no message is received within given time frame
            var receivedDataWithTimeout = await client.ReceiveAsync(TimeSpan.FromSeconds(5));

            // Determines whether client is still connected
            bool isConnected         = client.IsConnected();
            bool isConnectedWithPoll = client.IsConnected(true); // Use poll for a more accurate (but slower) result
        }