Socket.ReceiveMessageFrom Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation) |
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. |
ReceiveMessageFrom(Span<Byte>, SocketFlags, EndPoint, IPPacketInformation) |
Receives the specified number of bytes of data into the specified location of the data buffer, using the specified |
ReceiveMessageFrom(Byte[], Int32, Int32, SocketFlags, EndPoint, IPPacketInformation)
- Source:
- Socket.cs
- Source:
- Socket.cs
- Source:
- Socket.cs
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.
public:
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 * SocketFlags * EndPoint * IPPacketInformation -> 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
Parameters
- offset
- Int32
The position in the buffer
parameter to store the received data.
- size
- Int32
The number of bytes to receive.
- socketFlags
- SocketFlags
A bitwise combination of the SocketFlags values.
- remoteEP
- EndPoint
A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.
- ipPacketInformation
- IPPacketInformation
An IPPacketInformation holding address and interface information.
Returns
The number of bytes received.
Exceptions
offset
is less than 0.
-or-
offset
is greater than the length of buffer
.
-or-
size
is less than 0.
-or-
size
is greater than the length of the buffer
minus the value of the offset parameter.
socketFlags
is not a valid combination of values.
-or-
The LocalEndPoint property was not set.
-or-
The .NET Framework is running on an AMD 64-bit processor.
-or-
An error occurred when attempting to access the socket.
The Socket has been closed.
Remarks
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.
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. The socket must be initialized with the socket type set to Dgram or Raw before calling this method. This can be done when the socket is constructed using Socket.
For message-oriented sockets, an incoming message is placed into the buffer
parameter up to the total size specified in the size
parameter. The offset
parameter determines where in the buffer
the data is placed. The actual amount of data placed into the buffer
is returned by the ReceiveMessageFrom method.
The ReceiveMessageFrom method automatically method sets the PacketInformation socket option to true
the first time it is called for a given Socket. However, the returned IPPacketInformation object will only be valid for packets which arrive at the local computer after the socket option has been set. 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.
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.
An application can examine the ipPacketInformation
parameter if it needs to know if the datagram was sent using a unicast, multicast, or broadcast address.
Note
The AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.
Note
This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.
See also
- Connect(EndPoint)
- ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint)
- Available
- Shutdown(SocketShutdown)
- Close()
Applies to
ReceiveMessageFrom(Span<Byte>, SocketFlags, EndPoint, IPPacketInformation)
- Source:
- Socket.cs
- Source:
- Socket.cs
- Source:
- Socket.cs
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.
public:
int ReceiveMessageFrom(Span<System::Byte> buffer, System::Net::Sockets::SocketFlags % socketFlags, System::Net::EndPoint ^ % remoteEP, [Runtime::InteropServices::Out] System::Net::Sockets::IPPacketInformation % ipPacketInformation);
public int ReceiveMessageFrom (Span<byte> buffer, ref System.Net.Sockets.SocketFlags socketFlags, ref System.Net.EndPoint remoteEP, out System.Net.Sockets.IPPacketInformation ipPacketInformation);
member this.ReceiveMessageFrom : Span<byte> * SocketFlags * EndPoint * IPPacketInformation -> int
Public Function ReceiveMessageFrom (buffer As Span(Of Byte), ByRef socketFlags As SocketFlags, ByRef remoteEP As EndPoint, ByRef ipPacketInformation As IPPacketInformation) As Integer
Parameters
- socketFlags
- SocketFlags
A bitwise combination of the SocketFlags values.
- remoteEP
- EndPoint
A reference to an EndPoint of the same type as the endpoint of the remote host to be updated on successful receive.
- ipPacketInformation
- IPPacketInformation
An IPPacketInformation holding address and interface information.
Returns
The number of bytes received.
Exceptions
The Socket object has been closed.
The EndPoint remoteEP is null
.
The AddressFamily of the EndPoint used in ReceiveMessageFrom(Span<Byte>, SocketFlags, EndPoint, IPPacketInformation) needs to match the AddressFamily of the EndPoint used in SendTo.
The Socket object is not in blocking mode and cannot accept this synchronous call. You must call the Bind method before performing this operation.