本文整理汇总了C#中Socket.BeginReceiveMessageFrom方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.BeginReceiveMessageFrom方法的具体用法?C# Socket.BeginReceiveMessageFrom怎么用?C# Socket.BeginReceiveMessageFrom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.BeginReceiveMessageFrom方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BeginReceiveMessageFrom_NotSupported
public void BeginReceiveMessageFrom_NotSupported()
{
using (Socket sock = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
EndPoint ep = new IPEndPoint(IPAddress.Any, 0);
sock.Bind(ep);
byte[] buf = new byte[1];
Assert.Throws<PlatformNotSupportedException>(() => sock.BeginReceiveMessageFrom(buf, 0, buf.Length, SocketFlags.None, ref ep, null, null));
}
}
示例2: BeginReceiveMessageFrom_Helper
private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo, bool expectedToTimeout = false)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
EndPoint receivedFrom = new IPEndPoint(connectTo, port);
SocketFlags socketFlags = SocketFlags.None;
IPPacketInformation ipPacketInformation;
IAsyncResult async = serverSocket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);
// Behavior difference from Desktop: receivedFrom will _not_ change during the synchronous phase.
// IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
// Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
// Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
SocketUdpClient client = new SocketUdpClient(_log, serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(expectedToTimeout ? Configuration.FailingTestTimeout : Configuration.PassingTestTimeout);
if (!success)
{
throw new TimeoutException();
}
receivedFrom = new IPEndPoint(connectTo, port);
int received = serverSocket.EndReceiveMessageFrom(async, ref socketFlags, ref receivedFrom, out ipPacketInformation);
Assert.Equal(1, received);
Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));
IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
Assert.Equal(SocketFlags.None, socketFlags);
Assert.NotNull(ipPacketInformation);
Assert.Equal(connectTo, ipPacketInformation.Address);
// TODO: Move to NetworkInformation tests.
//Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, ipPacketInformation.Interface);
}
}
示例3: Socket_BeginReceiveMessageFromV4IPEndPointFromV4Client_Throws
[Fact] // Base case
// "The supplied EndPoint of AddressFamily InterNetwork is not valid for this Socket, use InterNetworkV6 instead."
public void Socket_BeginReceiveMessageFromV4IPEndPointFromV4Client_Throws()
{
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
socket.DualMode = false;
EndPoint receivedFrom = new IPEndPoint(IPAddress.Loopback, UnusedPort);
SocketFlags socketFlags = SocketFlags.None;
Assert.Throws<ArgumentException>(() =>
{
socket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);
});
}
示例4: Socket_BeginReceiveMessageFromDnsEndPoint_Throws
// "The parameter remoteEP must not be of type DnsEndPoint."
public void Socket_BeginReceiveMessageFromDnsEndPoint_Throws()
{
using (Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = socket.BindToAnonymousPort(IPAddress.IPv6Loopback);
EndPoint receivedFrom = new DnsEndPoint("localhost", port, AddressFamily.InterNetworkV6);
SocketFlags socketFlags = SocketFlags.None;
Assert.Throws<ArgumentException>(() =>
{
socket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);
});
}
}
示例5: BeginReceiveMessageFrom_Helper
private void BeginReceiveMessageFrom_Helper(IPAddress listenOn, IPAddress connectTo)
{
using (Socket serverSocket = new Socket(SocketType.Dgram, ProtocolType.Udp))
{
int port = serverSocket.BindToAnonymousPort(listenOn);
EndPoint receivedFrom = new IPEndPoint(connectTo, port);
SocketFlags socketFlags = SocketFlags.None;
IPPacketInformation ipPacketInformation;
IAsyncResult async = serverSocket.BeginReceiveMessageFrom(new byte[1], 0, 1, socketFlags, ref receivedFrom, null, null);
IPEndPoint remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
SocketUdpClient client = new SocketUdpClient(serverSocket, connectTo, port);
bool success = async.AsyncWaitHandle.WaitOne(500);
if (!success)
{
throw new TimeoutException();
}
receivedFrom = new IPEndPoint(connectTo, port);
int received = serverSocket.EndReceiveMessageFrom(async, ref socketFlags, ref receivedFrom, out ipPacketInformation);
Assert.Equal(1, received);
Assert.Equal<Type>(receivedFrom.GetType(), typeof(IPEndPoint));
remoteEndPoint = receivedFrom as IPEndPoint;
Assert.Equal(AddressFamily.InterNetworkV6, remoteEndPoint.AddressFamily);
Assert.Equal(connectTo.MapToIPv6(), remoteEndPoint.Address);
Assert.Equal(SocketFlags.None, socketFlags);
Assert.NotNull(ipPacketInformation);
Assert.Equal(connectTo, ipPacketInformation.Address);
// TODO: Move to NetworkInformation tests.
//Assert.Equal(NetworkInterface.IPv6LoopbackInterfaceIndex, ipPacketInformation.Interface);
}
}