Socket.ReceiveFrom Socket.ReceiveFrom Socket.ReceiveFrom Socket.ReceiveFrom Method

定义

接收数据报并存储源终结点。Receives a datagram and stores the source endpoint.

重载

ReceiveFrom(Byte[], SocketFlags, EndPoint) ReceiveFrom(Byte[], SocketFlags, EndPoint) ReceiveFrom(Byte[], SocketFlags, EndPoint)

使用指定的 SocketFlags 将数据报接收到数据缓冲区并存储终结点。Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint)

将数据报接收到数据缓冲区并存储终结点。Receives a datagram into the data buffer and stores the endpoint.

ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags 将指定的字节数接收到数据缓冲区并存储终结点。Receives the specified number of bytes into the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags 将指定的数据字节数接收到数据缓冲区的指定位置,并存储终结点。Receives the specified number of bytes of data into the specified location of the data buffer, using the specified SocketFlags, and stores the endpoint.

ReceiveFrom(Byte[], SocketFlags, EndPoint) ReceiveFrom(Byte[], SocketFlags, EndPoint) ReceiveFrom(Byte[], SocketFlags, EndPoint)

使用指定的 SocketFlags 将数据报接收到数据缓冲区并存储终结点。Receives a datagram into the data buffer, using the specified SocketFlags, and stores the endpoint.

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 *  -> int

参数

buffer
Byte[]

Byte 类型的数组,它是存储接收到的数据的位置。An array of type Byte that is the storage location for the received data.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

remoteEP
EndPoint EndPoint EndPoint EndPoint

通过引用传递的 EndPoint,表示远程服务器。An EndPoint, passed by reference, that represents the remote server.

返回

收到的字节数。The number of bytes received.

异常

buffernullbuffer is null.

- 或 --or- remoteEPnullremoteEP is null.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

调用堆栈中的调用方没有所需的权限。A caller in the call stack does not have the required permissions.

示例

下面的代码示例从远程主机接收无连接的数据报。The following code example receives a connectionless datagram from a remote host. SocketFlags传递给ReceiveFrom方法。SocketFlags are passed to the ReceiveFrom method.

static void ReceiveFrom2()
{
   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 'ReceiveFrom2

注解

方法ReceiveFrom将数据buffer读入参数, 返回成功读取的字节数, 并捕获从中发送数据的远程主机终结点。The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. 如果打算从未知主机或多台主机接收无连接的数据报, 此方法非常有用。This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

此重载只要求你提供接收缓冲区、必需SocketFlagsEndPoint和表示远程主机的。This overload only requires you to provide a receive buffer, the necessary SocketFlags, and an EndPoint that represents the remote host. 偏移量默认为 0, 大小默认为 buffer 参数的长度。The offset defaults to 0 and the size defaults to the length of the buffer parameter.

备注

在调用ReceiveFrom之前, 必须使用Bind方法将Socket显式绑定到本地终结点。Before calling ReceiveFrom, you must explicitly bind the Socket to a local endpoint using the Bind method. 否则, ReceiveFromSocketException引发。If you do not, ReceiveFrom will throw a SocketException.

对于无连接协议ReceiveFrom , 将读取接收到本地网络缓冲区中的第一个排队数据报。With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer. 如果接收的buffer数据报大于大小, 则该ReceiveFrom方法将尽可能多地填充buffer SocketException消息, 并引发。If the datagram you receive is larger than the size of buffer, the ReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. 如果使用的是不可靠的协议, 则过量的数据将丢失。If you are using an unreliable protocol, the excess data will be lost. 如果你使用的是可靠协议, 则该服务提供程序将保留多余的数据, 并且你可以通过使用足够大ReceiveFrom的缓冲区调用方法来检索这些数据。If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom method with a large enough buffer.

如果没有可供读取的数据, 则ReceiveFrom该方法将被阻止, 直到数据可用。If no data is available for reading, the ReceiveFrom method will block until data is available. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该ReceiveFrom方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

尽管ReceiveFrom适用于无连接协议, 但你也可以使用面向连接的协议。Although ReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. 如果选择这样做, 则必须先通过调用Connect方法建立远程主机连接, 或通过Accept调用方法接受传入的远程主机连接。If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept method. 如果在调用ReceiveFrom方法之前未建立或接受连接, 则将SocketException获得。If you do not establish or accept a connection before calling the ReceiveFrom method, you will get a SocketException. 还可以在调用ReceiveFrom方法之前, 为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom method. 在这两种情况下, ReceiveFrom方法将remoteEP忽略参数, 并仅从已连接或默认的远程主机接收数据。In either of these cases, the ReceiveFrom method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

使用面向连接的套接字ReceiveFrom时, 将读取尽可能多的数据, 最大可达buffer大小。With connection-oriented sockets, ReceiveFrom will read as much data as is available up to the size of buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该ReceiveFrom方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been Received, the ReceiveFrom method will complete immediately and return zero bytes.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

AddressFamilyReceiveFrom使用的需要与EndPoint中使用的匹配。SendTo AddressFamily EndPointThe AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint) ReceiveFrom(Byte[], EndPoint)

将数据报接收到数据缓冲区并存储终结点。Receives a datagram into the data buffer and stores the endpoint.

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[] *  -> int
Public Function ReceiveFrom (buffer As Byte(), ByRef remoteEP As EndPoint) As Integer

参数

buffer
Byte[]

类型 Byte 的数组,它是所接受的数据的存储位置。An array of type Byte that is the storage location for received data.

remoteEP
EndPoint EndPoint EndPoint EndPoint

通过引用传递的 EndPoint,表示远程服务器。An EndPoint, passed by reference, that represents the remote server.

返回

收到的字节数。The number of bytes received.

异常

buffernullbuffer is null.

- 或 --or- remoteEPnullremoteEP is null.

尝试访问套接字时出错。An error occurred when attempting to access the socket.

调用堆栈中的调用方没有所需的权限。A caller in the call stack does not have the required permissions.

示例

下面的代码示例从远程主机接收无连接的数据报。The following code example receives a connectionless datagram from a remote host.

static void ReceiveFrom1()
{
   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 'ReceiveFrom1

注解

方法ReceiveFrom将数据buffer读入参数, 返回成功读取的字节数, 并捕获从中发送数据的远程主机终结点。The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. 如果打算从未知主机或多台主机接收无连接的数据报, 此方法非常有用。This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

此重载仅要求你提供接收buffer, EndPoint以及表示远程主机的。This overload only requires you to provide a receive buffer, and an EndPoint that represents the remote host. 缓冲区偏移量默认为0。The buffer offset defaults to 0. 大小默认为buffer参数的长度socketFlags , 值默认为NoneThe size defaults to the length of the buffer parameter and the socketFlags value defaults to None.

备注

在调用ReceiveFrom之前, 必须使用Bind方法将Socket显式绑定到本地终结点。Before calling ReceiveFrom, you must explicitly bind the Socket to a local endpoint using the Bind method. 否则, ReceiveFromSocketException引发。If you do not, ReceiveFrom will throw a SocketException.

对于无连接协议ReceiveFrom , 将读取接收到本地网络缓冲区中的第一个排队数据报。With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer. 如果接收的buffer数据报大于大小, 则该ReceiveFrom方法将尽可能多地填充buffer SocketException消息, 并引发。If the datagram you receive is larger than the size of buffer, the ReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. 如果使用的是不可靠的协议, 则过量的数据将丢失。If you are using an unreliable protocol, the excess data will be lost. 如果你使用的是可靠协议, 则该服务提供程序将保留多余的数据, 并且你可以通过使用足够大ReceiveFrom的缓冲区调用方法来检索这些数据。If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom method with a large enough buffer.

如果没有可供读取的数据, 则ReceiveFrom该方法将被阻止, 直到数据可用。If no data is available for reading, the ReceiveFrom method will block until data is available. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该ReceiveFrom方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

尽管ReceiveFrom适用于无连接协议, 但你也可以使用面向连接的协议。Although ReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. 如果选择这样做, 则必须先通过调用Connect方法建立远程主机连接, 或通过Accept调用方法接受传入的远程主机连接。If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept method. 如果在调用ReceiveFrom方法之前未建立或接受连接, 则将SocketException获得。If you do not establish or accept a connection before calling the ReceiveFrom method, you will get a SocketException. 还可以在调用ReceiveFrom方法之前, 为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom method. 在这两种情况下, ReceiveFrom方法将remoteEP忽略参数, 并仅从已连接或默认的远程主机接收数据。In either of these cases, the ReceiveFrom method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

使用面向连接的套接字ReceiveFrom时, 将读取尽可能多的数据, 最大可达buffer大小。With connection-oriented sockets, ReceiveFrom will read as much data as is available up to the size of buffer. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该ReceiveFrom方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the ReceiveFrom method will complete immediately and return zero bytes.

备注

如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

AddressFamilyReceiveFrom使用的需要与EndPoint中使用的匹配。SendTo AddressFamily EndPointThe AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags 将指定的字节数接收到数据缓冲区并存储终结点。Receives the specified number of bytes into the data buffer, using the specified SocketFlags, and stores the endpoint.

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 *  -> int

参数

buffer
Byte[]

类型 Byte 的数组,它是所接受的数据的存储位置。An array of type Byte that is the storage location for received data.

size
Int32 Int32 Int32 Int32

要接收的字节数。The number of bytes to receive.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

remoteEP
EndPoint EndPoint EndPoint EndPoint

通过引用传递的 EndPoint,表示远程服务器。An EndPoint, passed by reference, that represents the remote server.

返回

收到的字节数。The number of bytes received.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

size 小于 0。size is less than 0.

- 或 --or- size 大于 buffer的长度。size is greater than the length of buffer.

socketFlags 不是有效的值组合。socketFlags is not a valid combination of values.

-or- 未设置 LocalEndPoint 属性。The LocalEndPoint property was not set.

-or- 访问 Socket 时出现操作系统错误。An operating system error occurs while accessing the Socket.

调用堆栈中的调用方没有所需的权限。A caller in the call stack does not have the required permissions.

示例

下面的代码示例从远程主机接收无连接的数据报。The following code example receives a connectionless datagram from a remote host. 缓冲区大小和SocketFlags被传递ReceiveFrom给方法。The buffer size, and SocketFlags are passed to the ReceiveFrom method.

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 'ReceiveFrom3

注解

方法ReceiveFrom将数据buffer读入参数, 返回成功读取的字节数, 并捕获从中发送数据的远程主机终结点。The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. 如果打算从未知主机或多台主机接收无连接的数据报, 此方法非常有用。This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

此重载只要求你提供接收缓冲区、要接收的字节数、必需SocketFlagsEndPoint以及表示远程主机的。This overload only requires you to provide a receive buffer, the number of bytes you want to receive, the necessary SocketFlags, and an EndPoint that represents the remote host. 缓冲区偏移量默认为0。The buffer offset defaults to 0.

对于无连接协议ReceiveFrom , 将读取接收到本地网络缓冲区中的第一个排队数据报。With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer. 如果接收的buffer数据报大于大小, 则该ReceiveFrom方法将尽可能多地填充buffer SocketException消息, 并引发。If the datagram you receive is larger than the size of buffer, the ReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. 如果使用的是不可靠的协议, 则过量的数据将丢失。If you are using an unreliable protocol, the excess data will be lost. 如果你使用的是可靠协议, 则该服务提供程序将保留多余的数据, 并且你可以通过使用足够大ReceiveFrom的缓冲区调用方法来检索这些数据。If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom method with a large enough buffer.

如果没有可供读取的数据, 则ReceiveFrom该方法将被阻止, 直到数据可用。If no data is available for reading, the ReceiveFrom method will block until data is available. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该ReceiveFrom方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

尽管ReceiveFrom适用于无连接协议, 但你也可以使用面向连接的协议。Although ReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. 如果选择这样做, 则必须先通过调用Connect方法建立远程主机连接, 或通过Accept调用方法接受传入的远程主机连接。If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept method. 如果在调用ReceiveFrom方法之前未建立或接受连接, 则将SocketException获得。If you do not establish or accept a connection before calling the ReceiveFrom method, you will get a SocketException. 还可以在调用ReceiveFrom方法之前, 为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom method. 在这两种情况下, ReceiveFrom方法将remoteEP忽略参数, 并仅从已连接或默认的远程主机接收数据。In either of these cases, the ReceiveFrom method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

使用面向连接的套接字ReceiveFrom时, 将读取与size参数指定的字节数最多的可用数据。With connection-oriented sockets, ReceiveFrom will read as much data as is available up to the number of bytes specified by the size parameter. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该ReceiveFrom方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the ReceiveFrom method will complete immediately and return zero bytes.

备注

在调用ReceiveFrom之前, 必须使用Bind方法将Socket显式绑定到本地终结点。Before calling ReceiveFrom, you must explicitly bind the Socket to a local endpoint using the Bind method. 否则, ReceiveFromSocketException引发。If you do not, ReceiveFrom will throw a SocketException. 如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

AddressFamilyReceiveFrom使用的需要与EndPoint中使用的匹配。SendTo AddressFamily EndPointThe AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联的枚举Accept:。Associated enumeration: Accept.

另请参阅

ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint)

使用指定的 SocketFlags 将指定的数据字节数接收到数据缓冲区的指定位置,并存储终结点。Receives the specified number of bytes of data into the specified location of the data buffer, using the specified SocketFlags, and stores the endpoint.

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 *  -> int

参数

buffer
Byte[]

类型 Byte 的数组,它是所接受的数据的存储位置。An array of type Byte that is the storage location for received data.

offset
Int32 Int32 Int32 Int32

buffer 参数中的位置,用于存储所接收的数据。The position in the buffer parameter to store the received data.

size
Int32 Int32 Int32 Int32

要接收的字节数。The number of bytes to receive.

socketFlags
SocketFlags SocketFlags SocketFlags SocketFlags

SocketFlags 值的按位组合。A bitwise combination of the SocketFlags values.

remoteEP
EndPoint EndPoint EndPoint EndPoint

通过引用传递的 EndPoint,表示远程服务器。An EndPoint, passed by reference, that represents the remote server.

返回

收到的字节数。The number of bytes received.

异常

buffernullbuffer is null.

-or- remoteEPnullremoteEP is null.

offset 小于 0。offset is less than 0.

-or- offset 大于 buffer 的长度。offset is greater than the length of buffer.

- 或 --or- size 小于 0。size is less than 0.

-or- size 大于 buffer 的长度减去偏移量参数的值。size is greater than the length of the buffer minus the value of the offset parameter.

socketFlags 不是有效的值组合。socketFlags is not a valid combination of values.

-or- 未设置 LocalEndPoint 属性。The LocalEndPoint property was not set.

-or- 尝试访问套接字时出错。An error occurred when attempting to access the socket.

示例

下面的代码示例从远程主机接收无连接的数据报。The following code example receives a connectionless datagram from a remote host. 偏移量、缓冲区大小和SocketFlags被传递ReceiveFrom给方法。The offset, buffer size, and SocketFlags are passed to the ReceiveFrom method.

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 'ReceiveFrom4

注解

方法ReceiveFrom将数据buffer读入参数, 返回成功读取的字节数, 并捕获从中发送数据的远程主机终结点。The ReceiveFrom method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. 如果打算从未知主机或多台主机接收无连接的数据报, 此方法非常有用。This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

对于无连接协议ReceiveFrom , 将读取接收到本地网络缓冲区中的第一个排队数据报。With connectionless protocols, ReceiveFrom will read the first enqueued datagram received into the local network buffer. 如果接收的buffer数据报大于大小, 则该ReceiveFrom方法将尽可能多地填充buffer SocketException消息, 并引发。If the datagram you receive is larger than the size of buffer, the ReceiveFrom method will fill buffer with as much of the message as is possible, and throw a SocketException. 如果使用的是不可靠的协议, 则过量的数据将丢失。If you are using an unreliable protocol, the excess data will be lost. 如果你使用的是可靠协议, 则该服务提供程序将保留多余的数据, 并且你可以通过使用足够大ReceiveFrom的缓冲区调用方法来检索这些数据。If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the ReceiveFrom method with a large enough buffer.

如果没有可供读取的数据, 则ReceiveFrom该方法将被阻止, 直到数据可用。If no data is available for reading, the ReceiveFrom method will block until data is available. 如果处于非阻止模式, 并且在协议堆栈缓冲区中没有可用的数据, 则该ReceiveFrom方法将立即完成并SocketException引发。If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the ReceiveFrom method will complete immediately and throw a SocketException. 您可以使用Available属性来确定数据是否可用于读取。You can use the Available property to determine if data is available for reading. 如果Available为非零, 则重试接收操作。When Available is non-zero, retry the receive operation.

尽管ReceiveFrom适用于无连接协议, 但你也可以使用面向连接的协议。Although ReceiveFrom is intended for connectionless protocols, you can use a connection-oriented protocol as well. 如果选择这样做, 则必须先通过调用Connect方法建立远程主机连接, 或通过Accept调用方法接受传入的远程主机连接。If you choose to do so, you must first either establish a remote host connection by calling the Connect method or accept an incoming remote host connection by calling the Accept method. 如果在调用ReceiveFrom方法之前未建立或接受连接, 则将SocketException获得。If you do not establish or accept a connection before calling the ReceiveFrom method, you will get a SocketException. 还可以在调用ReceiveFrom方法之前, 为无连接协议建立默认远程主机。You can also establish a default remote host for a connectionless protocol prior to calling the ReceiveFrom method. 在这两种情况下, ReceiveFrom方法将remoteEP忽略参数, 并仅从已连接或默认的远程主机接收数据。In either of these cases, the ReceiveFrom method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

使用面向连接的套接字ReceiveFrom时, 将读取与size参数指定的字节量最多的可用数据。With connection-oriented sockets, ReceiveFrom will read as much data as is available up to the amount of bytes specified by the size parameter. 如果远程主机Socket Shutdown使用方法关闭连接并且收到所有可用数据, 则该ReceiveFrom方法将立即完成并返回零字节。If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been Received, the ReceiveFrom method will complete immediately and return zero bytes.

备注

在调用ReceiveFrom之前, 必须使用Bind方法将Socket显式绑定到本地终结点。Before calling ReceiveFrom, you must explicitly bind the Socket to a local endpoint using the Bind method. 否则, ReceiveFromSocketException引发。If you do not, ReceiveFrom will throw a SocketException. 如果收到SocketException, 请SocketException.ErrorCode使用属性获取特定的错误代码。If you receive a SocketException, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后, 请参阅Windows 套接字版本 2 API 错误代码文档, 以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

备注

AddressFamilyReceiveFrom使用的需要与EndPoint中使用的匹配。SendTo AddressFamily EndPointThe AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息, 请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.

安全性

SocketPermission
用于接受来自网络的连接。for accepting connections from the network. 关联枚举:AcceptAssociated enumeration: Accept.

另请参阅

适用于