示例#1
0
        public async Task Connect2()
        {
            using var client = new EasyTcpClient();
            bool isConnected = await client.ConnectAsync("127.0.0.1", _port);

            Assert.IsTrue(isConnected);
        }
示例#2
0
        public async Task Connect1()
        {
            using var client = new EasyTcpClient();
            bool isConnected = await client.ConnectAsync(IPAddress.Any, _port);

            Assert.IsTrue(isConnected);
        }
示例#3
0
 /// <summary>
 /// Establish connection with remote host
 /// </summary>
 /// <param name="client"></param>
 /// <param name="ipAddress">ipAddress of remote host as string</param>
 /// <param name="port">port of remote host</param>
 /// <param name="socket">base socket for EasyTcpClient, new one is created when null</param>
 /// <returns>determines whether the client connected successfully</returns>
 /// <exception cref="ArgumentException">ipAddress is not a valid IPv4/IPv6 address</exception>
 public static async Task <bool> ConnectAsync(this EasyTcpClient client, string ipAddress, ushort port, Socket socket = null)
 {
     if (!IPAddress.TryParse(ipAddress, out IPAddress address))
     {
         throw new ArgumentException("Could not connect to remote host: ipAddress is not a valid IPv4/IPv6 address");
     }
     return(await client.ConnectAsync(address, port, socket));
 }
示例#4
0
 /// <summary>
 /// Try to connect to server until connected
 /// </summary>
 private async Task Connect(EasyTcpClient client, string ip, ushort port)
 {
     client.Dispose(); // Reset client
     while (!await client.ConnectAsync(ip, port))
     {
         await Task.Delay(ConnectTimeout);
     }
 }
 public async Task ConnectAsyncTest()
 {
     var           threadID   = Thread.CurrentThread.ManagedThreadId;
     EasyTcpClient easySocket = new EasyTcpClient()
     {
         Ip = "192.168.10.227", Port = 4545
     };
     await Task.Run(async() => await easySocket.ConnectAsync(dataReady).ConfigureAwait(false)).ConfigureAwait(false);
 }
示例#6
0
        public async Task ConnectIPAddressString()
        {
            var port = TestHelper.GetPort();

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

            Assert.IsTrue(await client.ConnectAsync("127.0.0.1", port));
            Assert.IsTrue(client.IsConnected());
        }
示例#7
0
        public async Task ConnectAsyncEndpoint()
        {
            var port = TestHelper.GetPort();

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

            Assert.IsTrue(await client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, port)));
            Assert.IsTrue(client.IsConnected());
        }
示例#8
0
        public static async Task <TestData> GetTestConnection(EasyTcpClient client = null, EasyTcpServer server = null)
        {
            var port = GetPort();

            server ??= new EasyTcpServer();
            client ??= new EasyTcpClient();
            server.Start(port);
            await client.ConnectAsync(IPAddress.Loopback, port);

            return(new TestData()
            {
                Client = client, Server = server
            });
        }
示例#9
0
        public async Task OnConnectClient()
        {
            var port = TestHelper.GetPort();

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

            int raised = 0;

            client.OnConnect += (_, c) => Interlocked.Increment(ref raised);
            await client.ConnectAsync(IPAddress.Loopback, port);

            Assert.AreEqual(1, raised);
        }
示例#10
0
        public async Task OnConnectServerMultipleConnections()
        {
            var port = TestHelper.GetPort();

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

            int raised = 0;

            server.OnConnect += (_, c) => Interlocked.Increment(ref raised);
            for (int i = 0; i < 5; i++)
            {
                using var client = new EasyTcpClient();
                await client.ConnectAsync(IPAddress.Loopback, port);
            }

            await TestHelper.WaitWhileFalse(() => raised == 5);

            Assert.AreEqual(raised, 5);
        }
示例#11
0
 /// <summary>
 /// Establishes connection with remote host
 /// </summary>
 /// <param name="client"></param>
 /// <param name="ipAddress">ipAddress of remote host</param>
 /// <param name="port">port of remote host</param>
 /// <param name="socket">socket for EasyTcpClient, new one is create when null</param>
 /// <returns>determines whether the client connected successfully</returns>
 public static async Task <bool> ConnectAsync(this EasyTcpClient client, IPAddress ipAddress, ushort port,
                                              Socket socket = null)
 => await client.ConnectAsync(new IPEndPoint(ipAddress, port), socket);
示例#12
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
        }