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


C# Socket.AcceptTaskAsync方法代码示例

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


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

示例1: AcceptLoopReuseAcceptContext

// ReSharper restore FunctionNeverReturns

        public async Task AcceptLoopReuseAcceptContext()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Loopback, 40040));
            socket.Listen(10);
            var context = new AcceptSocketAsyncContext();
            do
            {
                await socket.AcceptTaskAsync(context).ContinueWith(
                    async task =>
                        {
                            using (var socketAccept = task.Result)
                            {
                                var buffer = new byte[4096];
                                var received =
                                    await socketAccept.ReceiveTaskAsync(buffer, 0, buffer.Length, SocketFlags.None);
                                socketAccept.Shutdown(SocketShutdown.Receive);
                                await socketAccept.SendTaskAsync(buffer, 0, received, SocketFlags.None);
                                socketAccept.Shutdown(SocketShutdown.Both);
                                socketAccept.Close();
                            }
                        }
                    );
            } while (true);
// ReSharper disable FunctionNeverReturns
        }
开发者ID:kazuk,项目名称:SocketTaskAsync,代码行数:28,代码来源:BasicEchoServer.cs

示例2: AcceptReciveSendLoopFullSync

        public async Task AcceptReciveSendLoopFullSync()
        {
            var socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
            socket.Bind( new IPEndPoint( IPAddress.Loopback, 40020));
            socket.Listen(10);
            do
            {
                using (var acceptSocket = await socket.AcceptTaskAsync())
                {
                    var buffer = new byte[4096];

                    var received = await acceptSocket.ReceiveTaskAsync(buffer, 0, buffer.Length, SocketFlags.None);
                    acceptSocket.Shutdown(SocketShutdown.Receive);
                    await acceptSocket.SendTaskAsync( buffer,received, SocketFlags.None);
                    acceptSocket.Shutdown(SocketShutdown.Both);
                    acceptSocket.Close();
                }
            } while (true);
// ReSharper disable FunctionNeverReturns
        }
开发者ID:kazuk,项目名称:SocketTaskAsync,代码行数:20,代码来源:BasicEchoServer.cs

示例3: AcceptAsync

        public void AcceptAsync()
        {
            using (var socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp))
            {
                EndPoint localEp = new IPEndPoint(IPAddress.Loopback, 40001);
                socket.Bind(localEp);
                socket.Listen(10);
                var task = socket.AcceptTaskAsync();
                using (var connSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    var cont = task.ContinueWith(t =>
                        {
                            var acceptSocket = t.Result;
                            acceptSocket.RemoteEndPoint.Is(connSocket.LocalEndPoint);
                            acceptSocket.Close();
                        });
                    connSocket.Connect(localEp);

                    cont.Wait();
                }
            }
        }
开发者ID:kazuk,项目名称:SocketTaskAsync,代码行数:22,代码来源:UnitTest1.cs

示例4: AcceptAsyncWithAcceptSocketClose

 public void AcceptAsyncWithAcceptSocketClose()
 {
     using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
     {
         EndPoint localEp = new IPEndPoint(IPAddress.Loopback, 40000);
         socket.Bind(localEp);
         socket.Listen(10);
         var task = socket.AcceptTaskAsync();
         task.IsCompleted.IsFalse();
         socket.Close();
         try
         {
             task.Wait();
             Assert.Fail("unexpected Success");
         }
         catch (AggregateException e)
         {
             e.InnerExceptions.Count.Is(1);
             e.InnerExceptions[0].IsInstanceOf<AsyncSocketErrorException>()
                 .SocketError.Is(SocketError.OperationAborted);
         }
     }
 }
开发者ID:kazuk,项目名称:SocketTaskAsync,代码行数:23,代码来源:UnitTest1.cs


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