private void InitClients()
        {
            DerivedTcpClient client1 = new DerivedTcpClient()
            {
                ServerIp = "127.0.0.1", ServerPort = Config.RadarPort
            }, client2 = new DerivedTcpClient()
            {
                ServerIp = "127.0.0.1", ServerPort = Config.GnssPort
            };                                                                                                                                                                                                      //连接雷达和北斗,顺序无所谓

            client1.DataReceived += new DataReceivedEventHandler(Client_DataReceived);
            client2.DataReceived += new DataReceivedEventHandler(Client_DataReceived);
            Const.WriteConsoleLog("等待CLIENT1连接...");
            while (true)
            {
                try { client1.Connect(); }
                catch (Exception) { continue; }
                break;
            }
            Const.WriteConsoleLog(string.Format("{0}已连接\r\n等待CLIENT2连接...", client1.Name));
            while (true)
            {
                try { client2.Connect(); }
                catch (Exception) { continue; }
                break;
            }
            Const.WriteConsoleLog(string.Format("{0}已连接", client2.Name));
        }
Beispiel #2
0
        public void Active_Roundtrips()
        {
            using (var client = new DerivedTcpClient())
            {
                Assert.False(client.Active);

                client.Active = true;
                Assert.True(client.Active);
                Assert.Throws <SocketException>(() => client.Connect("anywhere", 0));

                client.Active = false;
                Assert.False(client.Active);
            }
        }
Beispiel #3
0
        public async Task ConnectAsync_DnsEndPoint_Success(int mode)
        {
            using (var client = new DerivedTcpClient())
            {
                Assert.False(client.Connected);
                Assert.False(client.Active);

                string host = System.Net.Test.Common.Configuration.Sockets.SocketServer.IdnHost;
                int    port = System.Net.Test.Common.Configuration.Sockets.SocketServer.Port;

                IPAddress[] addresses;
                switch (mode)
                {
                case 0:
                    await client.ConnectAsync(host, port);

                    break;

                case 1:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(addresses[0], port);

                    break;

                case 2:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(addresses, port);

                    break;

                case 3:
                    await Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, host, port, null);

                    break;

                case 4:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, addresses[0], port, null);

                    break;

                case 5:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await Task.Factory.FromAsync(client.BeginConnect, client.EndConnect, addresses, port, null);

                    break;

                case 6:
                    await client.ConnectAsync(host, port, CancellationToken.None);

                    break;

                case 7:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(addresses[0], port, CancellationToken.None);

                    break;

                case 8:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(new IPEndPoint(addresses[0], port));

                    break;

                case 9:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(new IPEndPoint(addresses[0], port), CancellationToken.None);

                    break;

                case 10:
                    addresses = await Dns.GetHostAddressesAsync(host);

                    await client.ConnectAsync(addresses, port, CancellationToken.None);

                    break;
                }

                Assert.True(client.Active);
                Assert.True(client.Connected);
                Assert.NotNull(client.Client);
                Assert.Same(client.Client, client.Client);

                using (NetworkStream s = client.GetStream())
                {
                    byte[] getRequest = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\n\r\n");
                    await s.WriteAsync(getRequest, 0, getRequest.Length);

                    Assert.NotEqual(-1, s.ReadByte()); // just verify we successfully get any data back
                }
            }
        }