本文整理汇总了C#中Socket.SendToAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.SendToAsync方法的具体用法?C# Socket.SendToAsync怎么用?C# Socket.SendToAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.SendToAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DualModeSendToAsync_IPEndPointToHost_Helper
private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer, bool expectedToTimeout = false)
{
int port;
Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp);
using (SocketUdpServer server = new SocketUdpServer(_log, listenOn, dualModeServer, out port))
{
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < Configuration.UDPRedundancy; i++)
{
using (ManualResetEvent waitHandle = new ManualResetEvent(false))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.UserToken = waitHandle;
args.Completed += AsyncCompleted;
bool async = client.SendToAsync(args);
if (async)
{
Assert.True(waitHandle.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");
}
Assert.Equal(1, args.BytesTransferred);
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
}
}
bool success = server.WaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout); // Make sure the bytes were received
if (!success)
{
throw new TimeoutException();
}
}
}
示例2: ClientSend
private void ClientSend(object state)
{
try
{
Socket socket = new Socket(_connectTo.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
for (int i = 0; i < Configuration.UDPRedundancy; i++)
{
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.RemoteEndPoint = new IPEndPoint(_connectTo, _port);
e.SetBuffer(new byte[1], 0, 1);
socket.SendToAsync(e);
}
}
catch (SocketException)
{
_serverSocket.Dispose(); // Cancels the test
}
}
示例3: Socket_SendToAsyncDnsEndPoint_Throws
[Fact] // Base case
// "The parameter remoteEP must not be of type DnsEndPoint."
public void Socket_SendToAsyncDnsEndPoint_Throws()
{
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new DnsEndPoint("localhost", UnusedPort);
args.SetBuffer(new byte[1], 0, 1);
Assert.Throws<ArgumentException>(() =>
{
socket.SendToAsync(args);
});
}
示例4: Socket_SendToAsyncV4IPEndPointToV4Host_Throws
[Fact] // Base case
public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws()
{
Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
args.SetBuffer(new byte[1], 0, 1);
bool async = socket.SendToAsync(args);
Assert.False(async);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Assert.Equal(SocketError.Fault, args.SocketError);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
// NOTE: on Linux, this API returns ENETUNREACH instead of EFAULT: this platform
// checks the family of the provided socket address before checking its size
// (as long as the socket address is large enough to store an address family).
Assert.Equal(SocketError.NetworkUnreachable, args.SocketError);
}
else
{
// NOTE: on other Unix platforms, this API returns EINVAL instead of EFAULT.
Assert.Equal(SocketError.InvalidArgument, args.SocketError);
}
}
示例5: SendToRecvFromAsync_Datagram_UDP
//.........这里部分代码省略.........
var receivedChecksums = new uint?[DatagramsToSend];
var receiveBuffer = new byte[DatagramSize];
int receivedDatagrams = -1;
Action<int, EndPoint> receiveHandler = null;
receiveHandler = (received, remote) =>
{
try
{
if (receivedDatagrams != -1)
{
Assert.Equal(DatagramSize, received);
Assert.Equal(rightEndpoint, remote);
int datagramId = (int)receiveBuffer[0];
Assert.Null(receivedChecksums[datagramId]);
receivedChecksums[datagramId] = Fletcher32.Checksum(receiveBuffer, 0, received);
receiverAck.Set();
Assert.True(senderAck.Wait(AckTimeout));
senderAck.Reset();
receivedDatagrams++;
if (receivedDatagrams == DatagramsToSend)
{
left.Dispose();
receiverFinished.SetResult(true);
return;
}
}
else
{
receivedDatagrams = 0;
}
left.ReceiveFromAsync(leftEventArgs, receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, receiveRemote, receiveHandler);
}
catch (Exception ex)
{
receiverFinished.SetException(ex);
}
};
receiveHandler(0, null);
var random = new Random();
var senderFinished = new TaskCompletionSource<bool>();
var sentChecksums = new uint[DatagramsToSend];
var sendBuffer = new byte[DatagramSize];
int sentDatagrams = -1;
Action<int> sendHandler = null;
sendHandler = sent =>
{
try
{
if (sentDatagrams != -1)
{
Assert.True(receiverAck.Wait(AckTimeout));
receiverAck.Reset();
senderAck.Set();
Assert.Equal(DatagramSize, sent);
sentChecksums[sentDatagrams] = Fletcher32.Checksum(sendBuffer, 0, sent);
sentDatagrams++;
if (sentDatagrams == DatagramsToSend)
{
right.Dispose();
senderFinished.SetResult(true);
return;
}
}
else
{
sentDatagrams = 0;
}
random.NextBytes(sendBuffer);
sendBuffer[0] = (byte)sentDatagrams;
right.SendToAsync(rightEventArgs, sendBuffer, 0, sendBuffer.Length, SocketFlags.None, leftEndpoint, sendHandler);
}
catch (Exception ex)
{
senderFinished.SetException(ex);
}
};
sendHandler(0);
Assert.True(receiverFinished.Task.Wait(TestTimeout));
Assert.True(senderFinished.Task.Wait(TestTimeout));
for (int i = 0; i < DatagramsToSend; i++)
{
Assert.NotNull(receivedChecksums[i]);
Assert.Equal(sentChecksums[i], (uint)receivedChecksums[i]);
}
}
示例6: Socket_SendToAsyncV4IPEndPointToV4Host_Throws
public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws()
{
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
socket.DualMode = false;
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
args.SetBuffer(new byte[1], 0, 1);
bool async = socket.SendToAsync(args);
Assert.False(async);
Assert.Equal(SocketError.Fault, args.SocketError);
}
示例7: DualModeSendToAsync_IPEndPointToHost_Helper
private void DualModeSendToAsync_IPEndPointToHost_Helper(IPAddress connectTo, IPAddress listenOn, bool dualModeServer)
{
int port;
ManualResetEvent waitHandle = new ManualResetEvent(false);
Socket client = new Socket(SocketType.Dgram, ProtocolType.Udp);
using (SocketUdpServer server = new SocketUdpServer(listenOn, dualModeServer, out port))
{
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.UserToken = waitHandle;
args.Completed += AsyncCompleted;
bool async = client.SendToAsync(args);
if (async)
{
Assert.True(waitHandle.WaitOne(5000), "Timeout while waiting for connection");
}
Assert.Equal(1, args.BytesTransferred);
if (args.SocketError != SocketError.Success)
{
throw new SocketException((int)args.SocketError);
}
bool success = server.WaitHandle.WaitOne(100); // Make sure the bytes were received
if (!success)
{
throw new TimeoutException();
}
}
}
示例8: Socket_SendToAsyncV4IPEndPointToV4Host_Throws_Unix
public void Socket_SendToAsyncV4IPEndPointToV4Host_Throws_Unix()
{
Socket socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, UnusedPort);
args.SetBuffer(new byte[1], 0, 1);
bool async = socket.SendToAsync(args);
Assert.False(async);
Assert.Equal(SocketError.NetworkUnreachable, args.SocketError);
}