本文整理汇总了C#中Socket.ReceiveAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ReceiveAsync方法的具体用法?C# Socket.ReceiveAsync怎么用?C# Socket.ReceiveAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.ReceiveAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReuseSocketAsyncEventArgs_SameInstance_MultipleSockets
public async Task ReuseSocketAsyncEventArgs_SameInstance_MultipleSockets()
{
using (var listen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listen.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listen.Listen(1);
Task<Socket> acceptTask = listen.AcceptAsync();
await Task.WhenAll(
acceptTask,
client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listen.LocalEndPoint).Port)));
using (Socket server = await acceptTask)
{
TaskCompletionSource<bool> tcs = null;
var args = new SocketAsyncEventArgs();
args.SetBuffer(new byte[1024], 0, 1024);
args.Completed += (_,__) => tcs.SetResult(true);
for (int i = 1; i <= 10; i++)
{
tcs = new TaskCompletionSource<bool>();
args.Buffer[0] = (byte)i;
args.SetBuffer(0, 1);
if (server.SendAsync(args))
{
await tcs.Task;
}
args.Buffer[0] = 0;
tcs = new TaskCompletionSource<bool>();
if (client.ReceiveAsync(args))
{
await tcs.Task;
}
Assert.Equal(1, args.BytesTransferred);
Assert.Equal(i, args.Buffer[0]);
}
}
}
}
示例2: Socket_SendReceiveAsync_Success
public async Task Socket_SendReceiveAsync_Success()
{
string path = GetRandomNonExistingFilePath();
var endPoint = new UnixDomainSocketEndPoint(path);
try
{
using (var server = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
using (var client = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified))
{
server.Bind(endPoint);
server.Listen(1);
await client.ConnectAsync(endPoint);
using (Socket accepted = await server.AcceptAsync())
{
var data = new byte[1];
for (int i = 0; i < 10; i++)
{
data[0] = (byte)i;
await accepted.SendAsync(new ArraySegment<byte>(data), SocketFlags.None);
data[0] = 0;
Assert.Equal(1, await client.ReceiveAsync(new ArraySegment<byte>(data), SocketFlags.None));
Assert.Equal(i, data[0]);
}
}
}
}
finally
{
try { File.Delete(path); }
catch { }
}
}
示例3: SendRecvAsync_0ByteReceive_Success
public static async Task SendRecvAsync_0ByteReceive_Success()
{
using (Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
listener.Bind(new IPEndPoint(IPAddress.Loopback, 0));
listener.Listen(1);
Task<Socket> acceptTask = listener.AcceptAsync();
await Task.WhenAll(
acceptTask,
client.ConnectAsync(new IPEndPoint(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndPoint).Port)));
using (Socket server = await acceptTask)
{
TaskCompletionSource<bool> tcs = null;
var ea = new SocketAsyncEventArgs();
ea.SetBuffer(Array.Empty<byte>(), 0, 0);
ea.Completed += delegate { tcs.SetResult(true); };
for (int i = 0; i < 3; i++)
{
tcs = new TaskCompletionSource<bool>();
// Have the client do a 0-byte receive. No data is available, so this should pend.
Assert.True(client.ReceiveAsync(ea));
Assert.Equal(0, client.Available);
// Have the server send 1 byte to the client.
Assert.Equal(1, server.Send(new byte[1], 0, 1, SocketFlags.None));
// The client should now wake up, getting 0 bytes with 1 byte available.
await tcs.Task;
Assert.Equal(0, ea.BytesTransferred);
Assert.Equal(SocketError.Success, ea.SocketError);
Assert.Equal(1, client.Available); // Due to #13778, this sometimes fails on macOS
// Receive that byte
Assert.Equal(1, client.Receive(new byte[1]));
Assert.Equal(0, client.Available);
}
}
}
}