本文整理汇总了C#中Socket.ReceiveMessageFromAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.ReceiveMessageFromAsync方法的具体用法?C# Socket.ReceiveMessageFromAsync怎么用?C# Socket.ReceiveMessageFromAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.ReceiveMessageFromAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Success
public void Success()
{
ManualResetEvent completed = new ManualResetEvent(false);
if (Socket.OSSupportsIPv4)
{
using (Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
receiver.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sender.Bind(new IPEndPoint(IPAddress.Loopback, 0));
sender.SendTo(new byte[1024], new IPEndPoint(IPAddress.Loopback, port));
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
args.SetBuffer(new byte[1024], 0, 1024);
args.Completed += OnCompleted;
args.UserToken = completed;
Assert.True(receiver.ReceiveMessageFromAsync(args));
Assert.True(completed.WaitOne(Configuration.PassingTestTimeout), "Timeout while waiting for connection");
Assert.Equal(1024, args.BytesTransferred);
Assert.Equal(sender.LocalEndPoint, args.RemoteEndPoint);
Assert.Equal(((IPEndPoint)sender.LocalEndPoint).Address, args.ReceiveMessageFromPacketInfo.Address);
sender.Dispose();
}
}
}
示例2: GetNonDefaultIPPacketInformation
private IPPacketInformation GetNonDefaultIPPacketInformation()
{
const int ReceiveTimeout = 5000;
using (var receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
using (var sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
var waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
UserToken = waitHandle
};
receiveArgs.SetBuffer(new byte[1], 0, 1);
receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));
// Send a few packets, in case they aren't delivered reliably.
for (int i = 0; i < Configuration.UDPRedundancy; i++)
{
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
}
Assert.True(waitHandle.WaitOne(ReceiveTimeout));
return receiveArgs.ReceiveMessageFromPacketInfo;
}
}
示例3: GetNonDefaultIPPacketInformation
private IPPacketInformation GetNonDefaultIPPacketInformation()
{
const int ReceiveTimeout = 5000;
using (var receiver = new Socket(SocketType.Dgram, ProtocolType.Udp))
using (var sender = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = receiver.BindToAnonymousPort(IPAddress.Loopback);
var waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs receiveArgs = new SocketAsyncEventArgs {
RemoteEndPoint = new IPEndPoint(IPAddress.Loopback, port),
UserToken = waitHandle
};
receiveArgs.SetBuffer(new byte[1], 0, 1);
receiveArgs.Completed += (_, args) => ((ManualResetEvent)args.UserToken).Set();
Assert.True(receiver.ReceiveMessageFromAsync(receiveArgs));
sender.SendTo(new byte[1], new IPEndPoint(IPAddress.Loopback, port));
Assert.True(waitHandle.WaitOne(ReceiveTimeout));
return receiveArgs.ReceiveMessageFromPacketInfo;
}
}
示例4: ReceiveMessageFromAsync_Helper
private void ReceiveMessageFromAsync_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
serverSocket.ReceiveTimeout = expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout;
int port = serverSocket.BindToAnonymousPort(listenOn);
ManualResetEvent waitHandle = new ManualResetEvent(false);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new IPEndPoint(connectTo, port);
args.SetBuffer(new byte[1], 0, 1);
args.Completed += AsyncCompleted;
args.UserToken = waitHandle;
bool async = serverSocket.ReceiveMessageFromAsync(args);
Assert.True(async);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
if (!waitHandle.WaitOne(serverSocket.ReceiveTimeout))
{
throw new TimeoutException();
}
Assert.Equal(1, args.BytesTransferred);
Assert.Equal<Type>(args.RemoteEndPoint.GetType(), typeof(IPEndPoint));
IPEndPoint remoteEndPoint = args.RemoteEndPoint as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
Assert.Equal(SocketFlags.None, args.SocketFlags);
Assert.NotNull(args.ReceiveMessageFromPacketInfo);
Assert.Equal(connectTo, args.ReceiveMessageFromPacketInfo.Address);
// TODO: Move to NetworkInformation tests.
// Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, args.ReceiveMessageFromPacketInfo.Interface);
}
}
示例5: Socket_ReceiveMessageFromAsyncDnsEndPoint_Throws
// "The parameter remoteEP must not be of type DnsEndPoint."
public void Socket_ReceiveMessageFromAsyncDnsEndPoint_Throws()
{
using (Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.RemoteEndPoint = new DnsEndPoint("localhost", port, AddressFamily.InterNetworkV6);
args.SetBuffer(new byte[1], 0, 1);
Assert.Throws<ArgumentException>(() =>
{
socket.ReceiveMessageFromAsync(args);
});
}
}
示例6: Socket_ReceiveMessageFromAsyncV4IPEndPointFromV4Client_Throws
[Fact] // Base case
// "The supplied EndPoint of AddressFamily InterNetwork is not valid for this Socket, use InterNetworkV6 instead."
public void Socket_ReceiveMessageFromAsyncV4IPEndPointFromV4Client_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);
Assert.Throws<ArgumentException>(() =>
{
socket.ReceiveMessageFromAsync(args);
});
}
示例7: ReceiveMessageFromAsync_NotSupported
public void ReceiveMessageFromAsync_NotSupported()
{
using (Socket sock = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
byte[] buf = new byte[1];
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
sock.Bind(ep);
SocketAsyncEventArgs args = new SocketAsyncEventArgs();
args.SetBuffer(buf, 0, buf.Length);
args.RemoteEndPoint = ep;
Assert.Throws<PlatformNotSupportedException>(() => sock.ReceiveMessageFromAsync(args));
}
}