本文整理汇总了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
}
示例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
}
示例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();
}
}
}
示例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);
}
}
}