Socket.ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation) Socket.ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation) Socket.ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation) Socket.ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation) Method


使用指定的 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 and packet information.

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



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

Int32 Int32 Int32 Int32

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

Int32 Int32 Int32 Int32

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

SocketFlags SocketFlags SocketFlags SocketFlags

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

EndPoint EndPoint EndPoint EndPoint

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

IPPacketInformation IPPacketInformation IPPacketInformation IPPacketInformation

保存地址和接口信息的 IPPacketInformationAn IPPacketInformation holding address and interface information.


收到的字节数。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- .NET Framework 运行在 AMD 64 位处理器上。The .NET Framework is running on an AMD 64-bit processor.

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

操作系统为 Windows 2000 或更低版本,而此方法需要在 Windows XP 中使用。The operating system is Windows 2000 or earlier, and this method requires Windows XP.


ReceiveMessageFrom方法将数据读入buffer参数,返回已成功读取的字节数和捕获从其发送数据,以及有关接收到的数据包的信息的远程主机终结点。The ReceiveMessageFrom 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, as well as information about the received packet.

ReceiveMessageFrom方法主要用于接收无连接的套接字上的消息数据。The ReceiveMessageFrom method is used primarily to receive message data on a connectionless socket. 必须知道套接字的本地地址。The socket's local address must be known. 此方法仅可用于数据报和原始套接字。This method can only be used with datagram and raw sockets. 必须使用套接字类型设置为初始化套接字DgramRaw之前调用此方法。The socket must be initialized with the socket type set to Dgram or Raw before calling this method. 这可以使用构造套接字时SocketThis can be done when the socket is constructed using Socket.

对于面向消息的套接字,传入的消息放入buffer最大总大小中指定的参数size参数。For message-oriented sockets, an incoming message is placed into the buffer parameter up to the total size specified in the size parameter. offset参数确定在何处buffer放置数据。The offset parameter determines where in the buffer the data is placed. 实际的数据量放入buffer返回的ReceiveMessageFrom方法。The actual amount of data placed into the buffer is returned by the ReceiveMessageFrom method.

ReceiveMessageFrom方法会自动方法设置PacketInformation套接字选项true第一次调用时为给定SocketThe ReceiveMessageFrom method automatically method sets the PacketInformation socket option to true the first time it is called for a given Socket. 但是,返回IPPacketInformation对象只能是有效的数据包到达本地计算机后已设置的套接字选项。However, the returned IPPacketInformation object will only be valid for packets which arrive at the local computer after the socket option has been set. 如果套接字在绑定到的本地终结点发送之间的数据包 (显式Bind方法或由其中一个隐式ConnectConnectAsyncSendTo,或SendToAsync方法) 和其第一次调用ReceiveMessageFrom方法中,调用ReceiveMessageFrom方法将返回无效IPPacketInformation这些数据包的对象。If a socket is sent packets between when it is bound to a local endpoint (explicitly by the Bind method or implicitly by one of the Connect, ConnectAsync, SendTo, or SendToAsync methods) and its first call to the ReceiveMessageFrom method, calls to ReceiveMessageFrom method will return invalid IPPacketInformation objects for these packets.

若要确保所有IPPacketInformation对象都有效,应用程序应设置PacketInformation套接字选项true绑定到本地终结点使用之前SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean)方法。To ensure that all IPPacketInformation objects are valid, an application should set the PacketInformation socket option to true before it is bound to a local endpoint using the SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) method.

应用程序可以检查ipPacketInformation参数,如果它需要知道是否使用单播、 多播或广播地址发送数据报。An application can examine the ipPacketInformation parameter if it needs to know if the datagram was sent using a unicast, multicast, or broadcast address.


AddressFamilyEndPoint中使用ReceiveFrom必须与匹配AddressFamilyEndPoint中使用SendToThe 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.


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