当前位置: 首页>>代码示例>>C#>>正文


C# TcpClient.Dispose方法代码示例

本文整理汇总了C#中TcpClient.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# TcpClient.Dispose方法的具体用法?C# TcpClient.Dispose怎么用?C# TcpClient.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TcpClient的用法示例。


在下文中一共展示了TcpClient.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConnectWithV4AndV6_Success

        public void ConnectWithV4AndV6_Success()
        {
            TcpListener listener = TcpListener.Create(TestPortBase + 3);
            listener.Start();
            IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v6Client = new TcpClient(AddressFamily.InterNetworkV6);
            v6Client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, TestPortBase + 3));

            TcpClient acceptedV6Client = listener.EndAcceptTcpClient(asyncResult);
            Assert.Equal(AddressFamily.InterNetworkV6, acceptedV6Client.Client.RemoteEndPoint.AddressFamily);
            Assert.Equal(AddressFamily.InterNetworkV6, v6Client.Client.RemoteEndPoint.AddressFamily);

            asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v4Client = new TcpClient(AddressFamily.InterNetwork);
            v4Client.Connect(new IPEndPoint(IPAddress.Loopback, TestPortBase + 3));

            TcpClient acceptedV4Client = listener.EndAcceptTcpClient(asyncResult);
            Assert.Equal(AddressFamily.InterNetworkV6, acceptedV4Client.Client.RemoteEndPoint.AddressFamily);
            Assert.Equal(AddressFamily.InterNetwork, v4Client.Client.RemoteEndPoint.AddressFamily);
            
            v6Client.Dispose();
            acceptedV6Client.Dispose();

            v4Client.Dispose();
            acceptedV4Client.Dispose();

            listener.Stop();
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:30,代码来源:AgnosticListenerTest.cs

示例2: ConnectWithV4AndV6_Success

        public void ConnectWithV4AndV6_Success()
        {
            int port;
            TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
            IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v6Client = new TcpClient(AddressFamily.InterNetworkV6);
            v6Client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, port));

            TcpClient acceptedV6Client = listener.EndAcceptTcpClient(asyncResult);
            Assert.Equal(AddressFamily.InterNetworkV6, acceptedV6Client.Client.RemoteEndPoint.AddressFamily);
            Assert.Equal(AddressFamily.InterNetworkV6, v6Client.Client.RemoteEndPoint.AddressFamily);

            asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v4Client = new TcpClient(AddressFamily.InterNetwork);
            v4Client.Connect(new IPEndPoint(IPAddress.Loopback, port));

            TcpClient acceptedV4Client = listener.EndAcceptTcpClient(asyncResult);
            Assert.Equal(AddressFamily.InterNetworkV6, acceptedV4Client.Client.RemoteEndPoint.AddressFamily);
            Assert.Equal(AddressFamily.InterNetwork, v4Client.Client.RemoteEndPoint.AddressFamily);

            v6Client.Dispose();
            acceptedV6Client.Dispose();

            v4Client.Dispose();
            acceptedV4Client.Dispose();

            listener.Stop();
        }
开发者ID:rainersigwald,项目名称:corefx,代码行数:30,代码来源:AgnosticListenerTest.cs

示例3: ConnectWithV4AndV6_Success

        public void ConnectWithV4AndV6_Success()
        {
            int port;
            TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
            IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v6Client = new TcpClient(AddressFamily.InterNetworkV6);
            v6Client.ConnectAsync(IPAddress.IPv6Loopback, port).GetAwaiter().GetResult();

            TcpClient acceptedV6Client = listener.EndAcceptTcpClient(asyncResult);

            asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient v4Client = new TcpClient(AddressFamily.InterNetwork);
            v4Client.ConnectAsync(IPAddress.Loopback, port).GetAwaiter().GetResult();

            TcpClient acceptedV4Client = listener.EndAcceptTcpClient(asyncResult);

            v6Client.Dispose();
            acceptedV6Client.Dispose();

            v4Client.Dispose();
            acceptedV4Client.Dispose();

            listener.Stop();
        }
开发者ID:SGuyGe,项目名称:corefx,代码行数:26,代码来源:AgnosticListenerTest.cs

示例4: ConnectWithV6_Success

        public void ConnectWithV6_Success()
        {
            int port;
            TcpListener listener = SocketTestExtensions.CreateAndStartTcpListenerOnAnonymousPort(out port);
            IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
            client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, port));

            TcpClient acceptedClient = listener.EndAcceptTcpClient(asyncResult);
            client.Dispose();
            acceptedClient.Dispose();
            listener.Stop();
        }
开发者ID:rainersigwald,项目名称:corefx,代码行数:14,代码来源:AgnosticListenerTest.cs

示例5: ConnectWithV6_Success

        public void ConnectWithV6_Success()
        {
            TcpListener listener = TcpListener.Create(TestPortBase + 2);
            listener.Start();
            IAsyncResult asyncResult = listener.BeginAcceptTcpClient(null, null);

            TcpClient client = new TcpClient(AddressFamily.InterNetworkV6);
            client.Connect(new IPEndPoint(IPAddress.IPv6Loopback, TestPortBase + 2));

            TcpClient acceptedClient = listener.EndAcceptTcpClient(asyncResult);
            client.Dispose();
            acceptedClient.Dispose();
            listener.Stop();
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:14,代码来源:AgnosticListenerTest.cs

示例6: Dispose_CancelsConnectAsync

        public async Task Dispose_CancelsConnectAsync(bool connectByName)
        {
            using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                // Set up a server socket to which to connect
                server.Bind(new IPEndPoint(IPAddress.Loopback, 0));
                server.Listen(1);
                var endpoint = (IPEndPoint)server.LocalEndPoint;

                // Connect asynchronously...
                var client = new TcpClient();
                Task connectTask = connectByName ?
                    client.ConnectAsync("localhost", endpoint.Port) :
                    client.ConnectAsync(endpoint.Address, endpoint.Port);

                // ...and hopefully before it's completed connecting, dispose.
                var sw = Stopwatch.StartNew();
                client.Dispose();

                // There is a race condition here.  If the connection succeeds before the
                // disposal, then the task will complete successfully.  Otherwise, it should
                // fail with an ObjectDisposedException.
                try
                {
                    await connectTask;
                }
                catch (ObjectDisposedException) { }
                sw.Stop();

                Assert.Null(client.Client); // should be nulled out after Dispose
            }
        }
开发者ID:SamuelEnglard,项目名称:corefx,代码行数:32,代码来源:TcpClientTest.cs

示例7: button1_Click_1

        /*  private void LayoutRoot_Unloaded(object sender, RoutedEventArgs e)
        {
            Sender.DetachDuplexOutputChannel();
        }*/
        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            textBox4.Text = "test";
            textBox2.Text = "";
            IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication();

            IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Intro English.jpg", FileMode.Open, fileStorage);
            //textBox4.Text = "1 step passed!";
            var image = new BitmapImage();
               // textBox4.Text = "2 step passed!";
            image.SetSource(stream);
            //textBox4.Text = "3 step passed!";
            image1.Source = image;
            //textBox4.Text = "4 step passed!";
            //stream.Seek(0,0);

            /*for (int i = 0; i < stream.Length; i++)
            {
                file[i] = stream.Read(
            }*/
            //string sim = "";
            byte[] img = new byte[stream.Length];
            long seekPos = stream.Seek(0, SeekOrigin.Begin);
            stream.Read(img, 0, img.Length);
            seekPos = stream.Seek(0, SeekOrigin.Begin);
               /* var cnt = 0;
            foreach (byte x in img)
            {
                textBox4.Text = cnt.ToString() + ":" + x.ToString();
                cnt++;
            }*/
            textBox4.Text = img[3000].ToString();

              /*  IPAddress ipa = IPAddress.Parse("127.0.0.1");
            IPEndPoint end = new IPEndPoint(ipa, 8001);*/
            TcpClient to_transmit = new TcpClient("127.0.0.1",8001);

               // to_transmit.Connect(textBox1.Text, 8001);
            //to_transmit.Connect(end);

            NetworkStream trans_stream = to_transmit.GetStream();
            trans_stream.Write(img, 0, img.Length);

            if (to_transmit.Connected != true) textBox4.Text = "Conn error!";

            byte[] resp = new byte[1000000];
            int max = trans_stream.Read(resp, 0, 1000000);

            for (int i = 0; i < max; i++) textBox2.Text += Convert.ToChar(resp[i]);

            trans_stream.Dispose();
            to_transmit.Dispose();

            //to_transmit.EndConnect();
            //textBox4.Text = stream.Length.ToString();
        }
开发者ID:adamekz,项目名称:PhoneOCR,代码行数:60,代码来源:MainPage.xaml.cs


注:本文中的TcpClient.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。