Socket.ReceiveMessageFromAsync(SocketAsyncEventArgs) Socket.ReceiveMessageFromAsync(SocketAsyncEventArgs) Socket.ReceiveMessageFromAsync(SocketAsyncEventArgs) Socket.ReceiveMessageFromAsync(SocketAsyncEventArgs) Method

定义

开始使用指定的 SocketFlags 将指定字节数的数据异步接收到数据缓冲区的指定位置,并存储终结点和数据包信息。Begins to asynchronously receive the specified number of bytes of data into the specified location in the data buffer, using the specified SocketFlags, and stores the endpoint and packet information.

public:
 bool ReceiveMessageFromAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool ReceiveMessageFromAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.ReceiveMessageFromAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function ReceiveMessageFromAsync (e As SocketAsyncEventArgs) As Boolean

参数

e
SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs

要用于此异步套接字操作的 SocketAsyncEventArgs 对象。The SocketAsyncEventArgs object to use for this asynchronous socket operation.

返回

如果 I/O 操作挂起,则为 truetrue if the I/O operation is pending. 操作完成时,将引发 e 参数的 Completed 事件。The Completed event on the e parameter will be raised upon completion of the operation.

如果 I/O 操作同步完成,则为 falsefalse if the I/O operation completed synchronously. 在这种情况下,将不会引发 e 参数的 Completed 事件,并且可能在方法调用返回后立即检查作为参数传递的 e 对象以检索操作的结果。In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.

异常

此方法需要 Windows XP 或更高版本。Windows XP or later is required for this method.

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

注解

ReceiveMessageFromAsync方法主要用于接收无连接的套接字上的消息数据。The ReceiveMessageFromAsync 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.

调用方必须设置SocketAsyncEventArgs.RemoteEndPoint属性设置为IPEndPoint,数据将从其接收的远程主机。The caller must set the SocketAsyncEventArgs.RemoteEndPoint property to the IPEndPoint of the remote host from which the data is to be received.

下列属性和事件上的System.Net.Sockets.SocketAsyncEventArgs成功调用此方法所需的对象:The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

调用方可能会设置SocketAsyncEventArgs.UserToken属性设置为在调用之前所需的任何用户状态对象ReceiveMessageFromAsync方法,以便可在回调方法中检索信息。The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the ReceiveMessageFromAsync method, so that the information will be retrievable in the callback method. 如果回调需要比单个对象的详细信息,可以创建一个小类来保存作为成员的其他所需的状态信息。If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

对于面向消息的套接字,传入的消息会放入缓冲区中直到缓冲区的总大小。For message-oriented sockets, an incoming message is placed into the buffer up to the total size of the buffer. SocketAsyncEventArgs.CountSocketAsyncEventArgs.Offset属性确定在缓冲区中数据的放置位置和数据量。The SocketAsyncEventArgs.Count and SocketAsyncEventArgs.Offset properties determine where in the buffer the data is placed and the amount of data.

ReceiveMessageFromAsync方法将自动设置PacketInformation套接字选项true第一次调用时为给定SocketThe ReceiveMessageFromAsync method automatically sets the PacketInformation socket option to true the first time it is called for a given Socket. 但是,IPPacketInformation对象只能是有效的数据包到达本地计算机后已设置的套接字选项。However, the IPPacketInformation object will only be valid for packets which arrive at the local computer after the socket option has been set. 如果套接字时套接字绑定到的本地终结点发送之间的数据包 (显式Bind方法或由其中一个隐式ConnectConnectAsyncSendTo,或SendToAsync方法) 和首次调用ReceiveMessageFromAsync方法,调用ReceiveMessageFromAsync方法将导致无效IPPacketInformation这些数据包的对象。If a socket is sent packets between when the socket is bound to a local endpoint (explicitly by the Bind method or implicitly by one of the Connect, ConnectAsync, SendTo, or SendToAsync methods) and the first call to the ReceiveMessageFromAsync method, calls to ReceiveMessageFromAsync method will result in 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 totrue before it is bound to a local endpoint using the SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) method.

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

适用于

另请参阅