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


C# Socket.DisconnectAsync方法代码示例

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


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

示例1: DisconnectAsync_Success

        public void DisconnectAsync_Success()
        {
            AutoResetEvent completed = new AutoResetEvent(false);

            IPEndPoint loopback = new IPEndPoint(IPAddress.Loopback, 0);
            using (var server1 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
            using (var server2 = SocketTestServer.SocketTestServerFactory(SocketImplementationType.Async, loopback))
            {
                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.Completed += OnCompleted;
                args.UserToken = completed;
                args.RemoteEndPoint = server1.EndPoint;
                args.DisconnectReuseSocket = true;

                using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    Assert.True(client.ConnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);

                    Assert.True(client.DisconnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);

                    args.RemoteEndPoint = server2.EndPoint;

                    Assert.True(client.ConnectAsync(args));
                    completed.WaitOne();
                    Assert.Equal(SocketError.Success, args.SocketError);
                }
            }
        }
开发者ID:Corillian,项目名称:corefx,代码行数:32,代码来源:DisconnectTest.cs

示例2: Success

        public void Success()
        {
            AutoResetEvent completed = new AutoResetEvent(false);

            if (Socket.OSSupportsIPv4)
            {
                using (SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase)))
                using (SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase + 1)))
                {
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.Completed += OnCompleted;
                    args.UserToken = completed;
                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, TestPortBase);
                    args.DisconnectReuseSocket = true;

                    Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    Assert.True(client.ConnectAsync(args));
                    Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
                    Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                    Assert.True(client.DisconnectAsync(args));
                    Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
                    Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                    args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, TestPortBase + 1);

                    Assert.True(client.ConnectAsync(args));
                    Assert.True(completed.WaitOne(5000), "Timed out while waiting for connection");
                    Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                    client.Dispose();
                }
            }
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:35,代码来源:DisconnectAsync.cs

示例3: DisconnectAsync_NonWindows_NotSupported

 public void DisconnectAsync_NonWindows_NotSupported()
 {
     using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
     {
         SocketAsyncEventArgs args = new SocketAsyncEventArgs();
         args.DisconnectReuseSocket = true;
         Assert.Throws<PlatformNotSupportedException>(() => client.DisconnectAsync(args));
     }
 }
开发者ID:Corillian,项目名称:corefx,代码行数:9,代码来源:DisconnectTest.cs

示例4: DisconnectAsync_Throws_PlatformNotSupported

        public void DisconnectAsync_Throws_PlatformNotSupported()
        {
            IPAddress address = null;
            if (Socket.OSSupportsIPv4)
            {
                address = IPAddress.Loopback;
            }
            else if (Socket.OSSupportsIPv6)
            {
                address = IPAddress.IPv6Loopback;
            }
            else
            {
                return;
            }

            int port;
            using (SocketTestServer.SocketTestServerFactory(address, out port))
            {
                var completed = new AutoResetEvent(false);

                var args = new SocketAsyncEventArgs
                {
                    UserToken = completed,
                    RemoteEndPoint = new IPEndPoint(address, port),
                    DisconnectReuseSocket = true
                };
                args.Completed += OnCompleted;

                var client = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                Assert.True(client.ConnectAsync(args));
                Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timed out while waiting for connection");
                Assert.Equal<SocketError>(SocketError.Success, args.SocketError);

                Assert.Throws<PlatformNotSupportedException>(() => client.DisconnectAsync(args));

                client.Dispose();
            }
        }
开发者ID:rainersigwald,项目名称:corefx,代码行数:39,代码来源:DisconnectAsync.cs


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