Export (0) Print
Expand All

Socket.ReceiveMessageFrom Method

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.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public int ReceiveMessageFrom(
	byte[] buffer,
	int offset,
	int size,
	ref SocketFlags socketFlags,
	ref EndPoint remoteEP,
	out IPPacketInformation ipPacketInformation
)

Parameters

buffer
Type: System.Byte[]

An array of type Byte that is the storage location for received data.

offset
Type: System.Int32

The position in the buffer parameter to store the received data.

size
Type: System.Int32

The number of bytes to receive.

socketFlags
Type: System.Net.Sockets.SocketFlags

A bitwise combination of the SocketFlags values.

remoteEP
Type: System.Net.EndPoint

An EndPoint, passed by reference, that represents the remote server.

ipPacketInformation
Type: System.Net.Sockets.IPPacketInformation

An IPPacketInformation holding address and interface information.

Return Value

Type: System.Int32
The number of bytes received.

ExceptionCondition
ArgumentNullException

buffer is null.

- or-

remoteEP is null.

ArgumentOutOfRangeException

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.

SocketException

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. See the Remarks section for more information.

ObjectDisposedException

The Socket has been closed.

NotSupportedException

The operating system is Windows 2000 or earlier, and this method requires Windows XP.

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.

NoteNote

The AddressFamily of the EndPoint used in ReceiveFrom needs to match the AddressFamily of the EndPoint used in SendTo.

NoteNote

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in the .NET Framework.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft