添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * int * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), size As Integer, socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

下面的代码示例从远程主机接收无连接数据报。 缓冲区大小 和 SocketFlags ReceiveFrom 传递给 方法。

static void ReceiveFrom3() IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() ); IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 ); Socket^ s = gcnew Socket( endPoint->Address->AddressFamily, SocketType::Dgram, ProtocolType::Udp ); // Creates an IPEndPoint to capture the identity of the sending host. IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 ); EndPoint^ senderRemote = safe_cast<EndPoint^>(sender); // Binding is required with ReceiveFrom calls. s->Bind( endPoint ); array<Byte>^ msg = gcnew array<Byte>(256); Console::WriteLine( "SWaiting to receive datagrams from client..." ); // This call blocks. s->ReceiveFrom( msg, msg->Length, SocketFlags::None, senderRemote ); s->Close(); public static void ReceiveFrom3() IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000); Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Creates an IPEndPoint to capture the identity of the sending host. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderRemote = (EndPoint)sender; // Binding is required with ReceiveFrom calls. s.Bind(endPoint); byte[] msg = new Byte[256]; Console.WriteLine("Waiting to receive datagrams from client..."); // This call blocks. s.ReceiveFrom(msg, msg.Length, SocketFlags.None, ref senderRemote); s.Close(); Public Shared Sub ReceiveFrom3() Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName()) Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000) Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) ' Creates an IPEndPoint to capture the identity of the sending host. Dim sender As New IPEndPoint(IPAddress.Any, 0) Dim senderRemote As EndPoint = CType(sender, EndPoint) ' Binding is required with ReceiveFrom calls. s.Bind(endPoint) Dim msg() As Byte = New [Byte](255) {} Console.WriteLine("Waiting to receive datagrams from client...") ' This call blocks. s.ReceiveFrom(msg, msg.Length, SocketFlags.None, senderRemote) s.Close() End Sub

方法 ReceiveFrom 将数据读入 buffer 参数,返回成功读取的字节数,并捕获从中发送数据的远程主机终结点。 如果要从未知主机或多个主机接收无连接数据报,此方法非常有用。

此重载只需提供接收缓冲区、要接收的字节数、必要的 SocketFlags 以及 EndPoint 表示远程主机的 。 缓冲区偏移量默认为 0。

使用无连接协议时, ReceiveFrom 会将接收到的第一个排队数据报读取到本地网络缓冲区中。 如果收到的数据报大于 的大小 buffer ,该方法 ReceiveFrom 将尽可能多地填充 buffer 消息,并引发 SocketException 。 如果使用不可靠的协议,则多余的数据将丢失。 如果使用可靠的协议,则服务提供程序将保留多余的数据,并且可以通过调用 ReceiveFrom 具有足够大缓冲区的 方法来检索这些数据。

如果没有可用于读取的数据,则 ReceiveFrom 方法将阻塞,直到数据可用。 如果处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 ReceiveFrom 方法将立即完成并引发 SocketException 。 可以使用 Available 属性来确定数据是否可用于读取。 如果 Available 为非零,请重试接收操作。

虽然 ReceiveFrom 适用于无连接协议,但也可以使用面向连接的协议。 如果选择这样做,则必须先通过调用 方法建立远程主机连接,或通过调用 Connect Accept 方法接受传入的远程主机连接。 如果在调用 ReceiveFrom 方法之前未建立或接受连接,则会收到 SocketException 。 在调用 ReceiveFrom 方法之前,还可以为无连接协议建立默认远程主机。

使用面向连接的套接字, ReceiveFrom 将读取最多参数 size 指定的字节数的可用数据。 如果远程主机关闭 Socket 了与 Shutdown 方法的连接,并且已收到所有可用数据,则 ReceiveFrom 该方法将立即完成并返回零个字节。

在调用 ReceiveFrom 之前,必须使用 方法将 显式绑定到 Socket 本地终结点 Bind 。 如果不这样做, ReceiveFrom 将引发 SocketException 。 如果收到 , SocketException 请使用 SocketException.ErrorCode 属性获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,获取错误的详细说明。

AddressFamily ReceiveFrom 所用 的 EndPoint 需要与 中 SendTo 所用 的 EndPoint 匹配 AddressFamily

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, int offset, int size, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * int * int * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), offset As Integer, size As Integer, socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer

下面的代码示例从远程主机接收无连接数据报。 偏移量、缓冲区大小 和 SocketFlags ReceiveFrom 传递给 方法。

static void ReceiveFrom4() IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() ); IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 ); Socket^ s = gcnew Socket( endPoint->Address->AddressFamily, SocketType::Dgram, ProtocolType::Udp ); // Creates an IpEndPoint to capture the identity of the sending host. IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 ); EndPoint^ senderRemote = safe_cast<EndPoint^>(sender); // Binding is required with ReceiveFrom calls. s->Bind( endPoint ); array<Byte>^ msg = gcnew array<Byte>(256); Console::WriteLine( "SWaiting to receive datagrams from client..." ); // This call blocks. s->ReceiveFrom( msg, 0, msg->Length, SocketFlags::None, senderRemote ); s->Close(); public static void ReceiveFrom4() IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000); Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Creates an IpEndPoint to capture the identity of the sending host. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderRemote = (EndPoint)sender; // Binding is required with ReceiveFrom calls. s.Bind(endPoint); byte[] msg = new Byte[256]; Console.WriteLine("Waiting to receive datagrams from client..."); // This call blocks. s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote); s.Close(); Public Shared Sub ReceiveFrom4() Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName()) Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000) Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) ' Creates an IpEndPoint to capture the identity of the sending host. Dim sender As New IPEndPoint(IPAddress.Any, 0) Dim senderRemote As EndPoint = CType(sender, EndPoint) ' Binding is required with ReceiveFrom calls. s.Bind(endPoint) Dim msg() As Byte = New [Byte](255) {} Console.WriteLine("Waiting to receive datagrams from client...") ' This call blocks. s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, senderRemote) s.Close() End Sub

方法 ReceiveFrom 将数据读入 buffer 参数,返回成功读取的字节数,并捕获从中发送数据的远程主机终结点。 如果要从未知主机或多个主机接收无连接数据报,此方法非常有用。

使用无连接协议时, ReceiveFrom 会将接收到的第一个排队数据报读取到本地网络缓冲区中。 如果收到的数据报大于 的大小 buffer ,该方法 ReceiveFrom 将尽可能多地填充 buffer 消息,并引发 SocketException 。 如果使用不可靠的协议,则多余的数据将丢失。 如果使用可靠的协议,则服务提供程序将保留多余的数据,并且可以通过调用 ReceiveFrom 具有足够大缓冲区的 方法来检索这些数据。

如果没有可用于读取的数据,则 ReceiveFrom 方法将阻塞,直到数据可用。 如果处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 ReceiveFrom 方法将立即完成并引发 SocketException 。 可以使用 Available 属性来确定数据是否可用于读取。 如果 Available 为非零,请重试接收操作。

虽然 ReceiveFrom 适用于无连接协议,但也可以使用面向连接的协议。 如果选择这样做,则必须先通过调用 方法建立远程主机连接,或通过调用 Connect Accept 方法接受传入的远程主机连接。 如果在调用 ReceiveFrom 方法之前未建立或接受连接,则会收到 SocketException 。 在调用 ReceiveFrom 方法之前,还可以为无连接协议建立默认远程主机。

使用面向连接的套接字, ReceiveFrom 将读取最多参数 size 指定的字节数的可用数据量。 如果远程主机关闭 Socket 了与 Shutdown 方法的连接,并且已收到所有可用数据,则 ReceiveFrom 该方法将立即完成并返回零个字节。

在调用 ReceiveFrom 之前,必须使用 方法将 显式绑定到 Socket 本地终结点 Bind 。 如果不这样做, ReceiveFrom 将引发 SocketException 。 如果收到 , SocketException 请使用 SocketException.ErrorCode 属性获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,了解错误的详细说明。

AddressFamily ReceiveFrom 所用 的 EndPoint 需要与 中 SendTo 所用 的 EndPoint 匹配 AddressFamily

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

public:
 int ReceiveFrom(Span<System::Byte> buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (Span<byte> buffer, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : Span<byte> * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Span(Of Byte), socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer
public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, System::Net::Sockets::SocketFlags socketFlags, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * System.Net.Sockets.SocketFlags * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), socketFlags As SocketFlags, ByRef remoteEP As EndPoint) As Integer
IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() ); IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 ); Socket^ s = gcnew Socket( endPoint->Address->AddressFamily, SocketType::Dgram, ProtocolType::Udp ); // Creates an IpEndPoint to capture the identity of the sending host. IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 ); EndPoint^ senderRemote = safe_cast<EndPoint^>(sender); // Binding is required with ReceiveFrom calls. s->Bind( endPoint ); array<Byte>^ msg = gcnew array<Byte>(256); Console::WriteLine( "Waiting to receive datagrams from client..." ); // This call blocks. s->ReceiveFrom( msg, SocketFlags::None, senderRemote ); s->Close(); public static void ReceiveFrom2() IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000); Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Creates an IpEndPoint to capture the identity of the sending host. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderRemote = (EndPoint)sender; // Binding is required with ReceiveFrom calls. s.Bind(endPoint); byte[] msg = new Byte[256]; Console.WriteLine("Waiting to receive datagrams from client..."); // This call blocks. s.ReceiveFrom(msg, SocketFlags.None, ref senderRemote); s.Close(); Public Shared Sub ReceiveFrom2() Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName()) Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000) Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) ' Creates an IpEndPoint to capture the identity of the sending host. Dim sender As New IPEndPoint(IPAddress.Any, 0) Dim senderRemote As EndPoint = CType(sender, EndPoint) ' Binding is required with ReceiveFrom calls. s.Bind(endPoint) Dim msg() As Byte = New [Byte](255) {} Console.WriteLine("Waiting to receive datagrams from client...") ' This call blocks. s.ReceiveFrom(msg, SocketFlags.None, senderRemote) s.Close() End Sub

方法 ReceiveFrom 将数据读入 buffer 参数,返回成功读取的字节数,并捕获从中发送数据的远程主机终结点。 如果要从未知主机或多个主机接收无连接数据报,此方法非常有用。

此重载只需提供接收缓冲区、必需的 SocketFlags EndPoint 表示远程主机的 。 偏移量默认为 0,大小默认为缓冲区参数的长度。

在调用 ReceiveFrom 之前,必须使用 方法将 显式绑定到 Socket 本地终结点 Bind 。 如果不这样做, ReceiveFrom 将引发 SocketException

使用无连接协议, ReceiveFrom 将读取接收到本地网络缓冲区中的第一个排队数据报。 如果收到的数据报大于 的大小 buffer ,则 ReceiveFrom 方法将填充 buffer 尽可能多的消息,并引发 SocketException 。 如果使用不可靠的协议,多余的数据将丢失。 如果使用可靠的协议,则服务提供程序将保留多余的数据,可以通过调用 ReceiveFrom 具有足够大缓冲区的方法来检索这些数据。

如果没有可用于读取的数据,方法 ReceiveFrom 将阻止数据,直到数据可用。 如果处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 ReceiveFrom 方法将立即完成并引发 SocketException 。 可以使用 Available 属性来确定数据是否可供读取。 如果 Available 为非零,请重试接收操作。

虽然 ReceiveFrom 适用于无连接协议,但也可以使用面向连接的协议。 如果选择这样做,必须先通过调用 Connect 方法建立远程主机连接,或通过调用 Accept 方法接受传入的远程主机连接。 如果在调用 ReceiveFrom 方法之前未建立或接受连接,将获得 SocketException 。 在调用 ReceiveFrom 方法之前,还可以为无连接协议建立默认远程主机。

使用面向连接的套接字, ReceiveFrom 读取的数据量与 可用大小一样多 buffer 。 如果远程主机关闭 Socket 了与 Shutdown 方法的连接,并且已收到所有可用数据,则 ReceiveFrom 该方法将立即完成并返回零个字节。

如果收到 , SocketException 请使用 SocketException.ErrorCode 属性获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,了解错误的详细说明。

AddressFamily ReceiveFrom 所用 的 EndPoint 需要与 中 SendTo 所用 的 EndPoint 匹配 AddressFamily

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪

public:
 int ReceiveFrom(Span<System::Byte> buffer, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (Span<byte> buffer, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : Span<byte> * EndPoint -> int
Public Function ReceiveFrom (buffer As Span(Of Byte), ByRef remoteEP As EndPoint) As Integer
public:
 int ReceiveFrom(cli::array <System::Byte> ^ buffer, System::Net::EndPoint ^ % remoteEP);
public int ReceiveFrom (byte[] buffer, ref System.Net.EndPoint remoteEP);
member this.ReceiveFrom : byte[] * EndPoint -> int
Public Function ReceiveFrom (buffer As Byte(), ByRef remoteEP As EndPoint) As Integer
IPHostEntry^ hostEntry = Dns::Resolve( Dns::GetHostName() ); IPEndPoint^ endPoint = gcnew IPEndPoint( hostEntry->AddressList[ 0 ],11000 ); Socket^ s = gcnew Socket( endPoint->Address->AddressFamily, SocketType::Dgram, ProtocolType::Udp ); // Creates an IPEndPoint to capture the identity of the sending host. IPEndPoint^ sender = gcnew IPEndPoint( IPAddress::Any,0 ); EndPoint^ senderRemote = safe_cast<EndPoint^>(sender); // Binding is required with ReceiveFrom calls. s->Bind( endPoint ); array<Byte>^ msg = gcnew array<Byte>(256); Console::WriteLine( "Waiting to receive datagrams from client..." ); // This call blocks. s->ReceiveFrom( msg, senderRemote ); s->Close(); public static void ReceiveFrom1() IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName()); IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000); Socket s = new Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Creates an IPEndPoint to capture the identity of the sending host. IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderRemote = (EndPoint)sender; // Binding is required with ReceiveFrom calls. s.Bind(endPoint); byte[] msg = new Byte[256]; Console.WriteLine("Waiting to receive datagrams from client..."); // This call blocks. s.ReceiveFrom(msg, ref senderRemote); s.Close(); Public Shared Sub ReceiveFrom1() Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName()) Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000) Dim s As New Socket(endPoint.Address.AddressFamily, SocketType.Dgram, ProtocolType.Udp) ' Creates an IPEndPoint to capture the identity of the sending host. Dim sender As New IPEndPoint(IPAddress.Any, 0) Dim senderRemote As EndPoint = CType(sender, EndPoint) ' Binding is required with ReceiveFrom calls. s.Bind(endPoint) Dim msg() As Byte = New [Byte](255) {} Console.WriteLine("Waiting to receive datagrams from client...") ' This call blocks. s.ReceiveFrom(msg, senderRemote) s.Close() End Sub

方法 ReceiveFrom 将数据读入 buffer 参数,返回成功读取的字节数,并捕获从中发送数据的远程主机终结点。 如果要从未知主机或多个主机接收无连接数据报,此方法非常有用。

此重载只需提供一个接收 buffer ,以及一个 EndPoint 表示远程主机的 。 缓冲区偏移量默认为 0。 大小默认为 参数的 buffer 长度, socketFlags 值默认为 None

在调用 ReceiveFrom 之前,必须使用 方法将 显式绑定到 Socket 本地终结点 Bind 。 如果不这样做, ReceiveFrom 将引发 SocketException

使用无连接协议, ReceiveFrom 将读取接收到本地网络缓冲区中的第一个排队数据报。 如果收到的数据报大于 的大小 buffer ,则 ReceiveFrom 方法将填充 buffer 尽可能多的消息,并引发 SocketException 。 如果使用不可靠的协议,多余的数据将丢失。 如果使用可靠的协议,则服务提供程序将保留多余的数据,可以通过调用 ReceiveFrom 具有足够大缓冲区的方法来检索这些数据。

如果没有可用于读取的数据,方法 ReceiveFrom 将阻止数据,直到数据可用。 如果处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 ReceiveFrom 方法将立即完成并引发 SocketException 。 可以使用 Available 属性来确定数据是否可供读取。 如果 Available 为非零,请重试接收操作。

虽然 ReceiveFrom 适用于无连接协议,但也可以使用面向连接的协议。 如果选择这样做,必须先通过调用 Connect 方法建立远程主机连接,或通过调用 Accept 方法接受传入的远程主机连接。 如果在调用 ReceiveFrom 方法之前未建立或接受连接,将获得 SocketException 。 在调用 ReceiveFrom 方法之前,还可以为无连接协议建立默认远程主机。

使用面向连接的套接字, ReceiveFrom 读取的数据量与 可用大小一样多 buffer 。 如果远程主机关闭 Socket 了与 Shutdown 方法的连接,并且已收到所有可用数据,则 ReceiveFrom 该方法将立即完成并返回零个字节。

如果收到 , SocketException 请使用 SocketException.ErrorCode 属性获取特定的错误代码。 获取此代码后,请参阅 Windows 套接字版本 2 API 错误代码 文档,了解错误的详细说明。

AddressFamily ReceiveFrom 所用 的 EndPoint 需要与 中 SendTo 所用 的 EndPoint 匹配 AddressFamily

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪